From: Billy G. A. <bil...@mu...> - 2003-11-05 04:39:14
|
Karsten Hilbert wrote: >Hi, > >I am trying to use the IN operator with a tuple of length 1 >(no, I can't change to = since I don't know in advance whether >the tuple will be of size 1 or longer). > >If I simply pass the tuple to cursor.execute() it gets >unrolled producing one of two errors: > if length == 1: > select * from ... where ... IN scalar_value; > if length > 1: > Exception: not all arguments converted > >If I wrap the tuple in another tuple or list of length 1, eg. >(my_tuple,) or [my_tuple] then cursor.execute does recognize >that it is supposed to build a list suitable for IN. However, >this fails with tuples of size 1: > >select * from ... where ... IN (scalar_value,); > >tuples with size > 1 work fine: > >select * from ... where ... IN (scalar, scalar); > >I did upgrade to 2.4 since the changelog said something about >a fix to the parameter handling concerned with IN operators. >That didn't help, unfortunately. > >And this line in PgSQL -> cursor.execute() seems to be the >root of the evil: > > self.res = self.conn.conn.query(_qstr % parms) > >What am I to do ? > >Karsten > > 1. Get the latest PgSQL.py from CVS. It contains additional fixes for using the IN operator. 2. If the list to use in the IN statement is the only parameter, then you will need to use the following syntax: <assume inparms s the tuple to use in the IN statement> cursor.execute("select * from ... where ... n %s", (inparms, )) This is necessary due to the way python handles varying parameters. If parms is a list, it is assumed to be multiple parameters to be quoted and placed into the query. If the inparms was one of many parameters being sent to the query, it would not need to be wrapped in a tuple. -- ____ | Billy G. Allie | Domain....: Bil...@mu... | /| | 7436 Hartwell | MSN.......: B_G...@em... |-/-|----- | Dearborn, MI 48126| |/ |LLIE | (313) 582-1540 | |