psycopg2

Tim Roberts timr at probo.com
Sun Feb 3 00:22:19 EST 2008


Andre' John <joa at hrz.tu-chemnitz.de> wrote:
>
>I am trying to do this for a Postgresql database:
>
>conn = psycopg2.connect('host=localhost')
>cur = conn.cursor()
>cur.execute("SELECT * FROM names WHERE name=%s", ['S'])
>
>, which doesn't work, and neither does
>
>cur.execute("SELECT * FROM names WHERE name='%s'", ['S'])

Psycopg requires that the parameters be passed as a tuple, not a list.
Also, psycopg will do the quoting for you.  You don't do it.  So this is
what you want:

    cur.execute("SELECT * FROM names WHERE name=%s", ('S',) )

Note that the extra comma is required in Python to make a one-element
tuple.
-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-list mailing list