[DB-SIG] execute a query with psycopg2

Christoph Zwerschke cito at online.de
Thu Feb 7 22:51:53 CET 2008


Michael Castleton schrieb:
 > but when I try the following I get 'TypeError: unindexable object':
 >
 > events = [1001,1002,1003]
 > for num in events:
 > cur2.execute("SELECT date,lat1,lon1 FROM table WHERE eventid = 
%s",(num,))
 >
 > I have tried all manor of variation I can think of - (%s,)(num,) or '%s',
 > num or %(what)s,{what:(num,)} etc..
 >
 > Any ideas on what I'm doing wrong?

psycopg2 uses paramstyle = pyformat, so %(what)s would be the right 
syntax. Also, since you are calling execute (not executemany), you must 
not use a sequence as parameters, but only one dict, like that:

cur2.execute("SELECT ... WHERE eventid = %(num)s", dict(num=num))

-- Christoph


More information about the DB-SIG mailing list