From j.beyer at web.de Fri Dec 28 15:44:10 2007 From: j.beyer at web.de (Joerg Beyer) Date: Fri, 28 Dec 2007 15:44:10 +0100 Subject: [DB-SIG] how do I execute a query likte 'select * from mytable where id in (1, 2, 3)'? Message-ID: <47750BBA.5030201@web.de> Hello, 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, ...)" Any suggestions? thanks in advance Joerg From carsten at uniqsys.com Sat Dec 29 08:13:55 2007 From: carsten at uniqsys.com (Carsten Haese) Date: Sat, 29 Dec 2007 02:13:55 -0500 Subject: [DB-SIG] how do I execute a query likte 'select * from mytable where id in (1, 2, 3)'? In-Reply-To: <47750BBA.5030201@web.de> References: <47750BBA.5030201@web.de> Message-ID: <20071229070733.M17363@uniqsys.com> On Fri, 28 Dec 2007 15:44:10 +0100, Joerg Beyer wrote > Hello, > > 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, ...)" If the list is short enough that an IN query is feasible, you'll want to do something like this: sql = "select * from mytable where id in ("+",".join("?" for _ in mylist)+")" cur.execute(sql, tuple(mylist)) (This assumes that your unspecified API module for your unspecified database engine uses question marks as parameter markers. Adjust the parameter marker to %s if necessary.) If the list comes from another table, you should probably use a sub-query or rewrite your query to use a join instead. Hope this helps, -- Carsten Haese http://informixdb.sourceforge.net From haraldarminmassa at gmail.com Sat Dec 29 11:37:14 2007 From: haraldarminmassa at gmail.com (Harald Armin Massa) Date: Sat, 29 Dec 2007 11:37:14 +0100 Subject: [DB-SIG] how do I execute a query likte 'select * from mytable where id in (1, 2, 3)'? In-Reply-To: <47750BBA.5030201@web.de> References: <47750BBA.5030201@web.de> Message-ID: <7be3f35d0712290237w6ecbf1fblacbd845aae81deb6@mail.gmail.com> 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! From fog at initd.org Sun Dec 30 01:24:54 2007 From: fog at initd.org (Federico Di Gregorio) Date: Sun, 30 Dec 2007 01:24:54 +0100 Subject: [DB-SIG] how do I execute a query likte 'select * from mytable where id in (1, 2, 3)'? In-Reply-To: <7be3f35d0712290237w6ecbf1fblacbd845aae81deb6@mail.gmail.com> References: <47750BBA.5030201@web.de> <7be3f35d0712290237w6ecbf1fblacbd845aae81deb6@mail.gmail.com> Message-ID: <1198974294.4184.4.camel@mila.office.dinunzioedigregorio> Il giorno sab, 29/12/2007 alle 11.37 +0100, Harald Armin Massa ha scritto: > 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. 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", {'what': (1,2,3)}) federico -- 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 Type: application/pgp-signature Size: 189 bytes Desc: Questa =?ISO-8859-1?Q?=E8?= una parte del messaggio firmata digitalmente Url : http://mail.python.org/pipermail/db-sig/attachments/20071230/b42c80ff/attachment.pgp From j.beyer at web.de Sat Dec 29 21:53:11 2007 From: j.beyer at web.de (Joerg Beyer) Date: Sat, 29 Dec 2007 21:53:11 +0100 Subject: [DB-SIG] how do I execute a query likte 'select * from mytable where id in (1, 2, 3)'? In-Reply-To: <7be3f35d0712290237w6ecbf1fblacbd845aae81deb6@mail.gmail.com> References: <47750BBA.5030201@web.de> <7be3f35d0712290237w6ecbf1fblacbd845aae81deb6@mail.gmail.com> Message-ID: <4776B3B7.2040207@web.de> Harald Armin Massa schrieb: > 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? I am using MySQL. > > 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. the MySQL function "in()" seems to be what "any()" is for PostgreSQL, see http://dev.mysql.com/doc/refman/5.0/en/func-op-summary-ref.html http://www.postgresql.org/docs/8.2/static/functions-aggregate.html The missing trick was to use a python list as a an argument to the query. It is obvious now. Thanks for the hint Joerg > Best wishes, > > Harald > > -- skype: j.beyer, jabber: j.beyer at web.de gpg: 2CFB B5F2 988E 8897 06DB 2D50 F128 D2D6 F1B2 2099