2

Possible Duplicate:
python list in sql query as parameter

Consider this (using apsw here):

s = ["A", "B", "C"]
c.execute("SELECT foo.y FROM foo WHERE foo.x in (?)", (s, ))

This doesn't work, because a binding parameter cannot be a list. I want to bind a list of strings to ?. I know how to build the appropriate query-string manually, but I wonder if there is a way to do this with bindings.

Community
  • 1
  • 1
Björn Pollex
  • 72,744
  • 28
  • 189
  • 274

2 Answers2

1

Going with the multiple question marks idea by Fabian, how about

c.execute("SELECT foo.y FROM foo WHERE foo.x in (%s)" % ', '.join('?' * len(s)), s)
Marius Gedminas
  • 10,676
  • 3
  • 38
  • 38
0

I had this problem around 4 years ago, and then I found out it was impossible to bind lists to sql (i was using mssql server and ODBC provider, but also considered direct sql calls)
In my case i was just building the queries manually and it was efficient. In case you have a very long list of values you will have to create another table, populate it in runtime and join with it in your sql.

ULysses
  • 990
  • 4
  • 9