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
  • 75,346
  • 28
  • 201
  • 283

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
  • 11,010
  • 4
  • 41
  • 39
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
  • 978
  • 4
  • 9