[Baypiggies] newbie troubles with dbapi parameter formatting

Ken-ichi kenichi.ueda at gmail.com
Fri Mar 24 00:37:12 CET 2006


Hi all,

I'm new, live up in Berkeley, and am trying to learn Python.  I'm 
currently struggling with database interaction, specifically with 
PostgreSQL and the Psycopg2 module, which implements the Python DB API 
spec.  I'm trying to do something like

cursor.execute('SELECT * FROM table WHERE column IN (%s)', (a_list,))

where a_list is a list of strings.  I've tried something like this:

cursor.execute('SELECT * FROM table WHERE column IN (%s)', (', 
'.join(a_list),))

and

cursor.execute('SELECT * FROM table WHERE column IN (%s)', ("', 
'".join(a_list),))

but they don't single-quote the strings properly (it thinks the comma is 
a separate string, and so encloses it in single-quote again for 
something like ('val1'',''val2'',''val3').

My working solution is to just format the query before feeding it to the 
cursor, but I feel like the execute() method should support arbitrary 
lists of parameters.  I just can't find any docs on it.  Does anyone 
know how to do this?  Am I thinking about this in completely the wrong way?

Thanks.

-Ken-ichi


More information about the Baypiggies mailing list