[DB-SIG] how do I execute a query likte 'select * from mytable where id in (1, 2, 3)'?

Harald Armin Massa haraldarminmassa at gmail.com
Sat Dec 29 11:37:14 CET 2007


Joerg,

> maybe this is obvious, but I don't know how to execute a query like:
> select * from mytable where id in (1,2,3)
> from python. The difficult part is the set in the where clause, the "in
> (a,b,c,d, ...)"


WHICH database are you talking about?

With Oracle, you are on your own, that is, you have to do stringmagic
to create the SQL.

With PostgreSQL and PSYCOPG2 you can use lists:

cs.execute("select * from s2e where element = any (%(what)s)",
dict(what=[1,2,3]))

using PostgreSQL arrays and the "any" function.

Best wishes,

Harald


-- 
GHUM Harald Massa
persuadere et programmare
Harald Armin Massa
Spielberger Straße 49
70435 Stuttgart
0173/9409607
fx 01212-5-13695179
-
EuroPython 2008 will take place in Vilnius, Lithuania - Stay tuned!


More information about the DB-SIG mailing list