dbapi2 select where IN (...)

Gerhard Häring gh at ghaering.de
Mon Nov 30 16:13:16 CET 2009


yota.news at gmail.com wrote:
> hello,
> 
> I couldn't find how the dbapi2 planned to handle the sql IN statement.
> 
> ex :
> SELECT * FROM table WHERE num IN (2,3,8,9);
> 
> I'd be glad to take advantage of the ? mechanism, but what about
> tuples !
> 
> execute("""SELECT * FROM table WHERE num IN ?;""" ,
> ((2,3,8,9),))    ...fail... [...]

You cannot use parameter binding when the number of parameters is
unknown in advance. So you'll have to create this part of the SQL query
differently.

For example:

ids = [2, 3, 8, 9]
in_clause = " (" + ",".join([str(id) for id in ids]) + ")"
query = "select * from table where num in" + in_clause

-- Gerhard




More information about the Python-list mailing list