0

There is the executemany and some other methods I have found for inserting data. However execute many doesn't work for SELECT.

In the sql format, the IN and VALUES can be used, for example:

SELECT col1,col2 FROM Table1 WHERE col3 IN (VALUES (1),(2),(3) )

How can I perform this operation in python with an array?

Here was an attempt, but has an issue binding:

dataArr = (1,2,3)
c.execute( "SELECT col1,col2 FROM Table1 WHERE col3 IN (?) ",(dataArr,) )
ret = c.fetchall()

I have also tried the string contcatenation from this question but it flattens the rows into a single array. The benefit is that it performs 2~3x faster than a loop for me, which is the main concern.

001001
  • 530
  • 1
  • 4
  • 13

1 Answers1

0

try with this :

args=['1','2','3']

sql="select col1,col2 FROM Table1 WHERE col3 IN in ({seq})".format(seq=','.join(['?']*len(args)))

rows=cur.execute(sql, args)

for row in rows:
    print(row)
GiovaniSalazar
  • 1,999
  • 2
  • 8
  • 15