[DB-SIG] execute a query with psycopg2

Mike Meyer mwm at mired.org
Thu Feb 7 22:20:16 CET 2008

On Thu, 7 Feb 2008 12:51:56 -0800 (PST) Michael Castleton <fatuheeva at yahoo.com> wrote:

> Hello,
> I think I am missing something very simple. The following command works fine
> if there is no variable substitution:
> cur2.execute("SELECT date,lat1,lon1 FROM table WHERE eventid = 1001;")
> 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?

In generally, not reading
http://catb.org/~esr/faqs/smart-questions.html (or maybe not

In particular, you're not telling us which version of python you're
using, which database (and version) you're using, and which interface
module you're using. The correct way to write your query depends on

Mike Meyer <mwm at mired.org>		http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.

More information about the DB-SIG mailing list