[DB-SIG] how do I execute a query likte 'select * from mytable where id in (1, 2, 3)'?
Federico Di Gregorio
fog at initd.org
Sun Dec 30 01:24:54 CET 2007
Il giorno sab, 29/12/2007 alle 11.37 +0100, Harald Armin Massa ha
> > 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)",
> using PostgreSQL arrays and the "any" function.
psycopg2 supports direct conversion of tuples to IN clauses, doing
quoting on the arguments. Just do:
cs.execute("select * from s2e where element IN %(what)s",
Federico Di Gregorio http://people.initd.org/fog
Debian GNU/Linux Developer fog at debian.org
INIT.D Developer fog at initd.org
If a process is potentially good, but 90%+ of the time smart and
well-intentioned people screw it up, then it's a bad process.
-- Steve Yegge
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Size: 189 bytes
Desc: Questa =?ISO-8859-1?Q?=E8?= una parte del messaggio
Url : http://mail.python.org/pipermail/db-sig/attachments/20071230/b42c80ff/attachment.pgp
More information about the DB-SIG