dbapi2 select where IN (...)

Y

yota.news

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...

what would be the most pythonic way to do this ?
 
G

Gerhard Häring

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top