[DB-SIG] execute a query with psycopg2

Michael Castleton fatuheeva at yahoo.com
Fri Feb 8 02:00:19 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


First I apologize for not making my situation perfectly clear. I am running
python 2.4, talking to a postgres v8? database with psycopg2.
Second, I still don't have a good handle on paramstyle=pyformat but I
was able to make the query work using: 
%(num)s", dict(num=num)

Thanks for the help Christoph,

Mike
-- 
View this message in context: http://www.nabble.com/execute-a-query-with-psycopg2-tp15343055p15347390.html
Sent from the Python - db-sig mailing list archive at Nabble.com.



More information about the DB-SIG mailing list