psycopg2 gets confused by % characters and strict pyformat mode

Alejandro Dubrovsky <[email protected]>
Newsgroups gmane.comp.python.db.psycopg.devel
Message-ID <[email protected]>
I've got a script that takes SQL input from the user, then tacks
something on at the end and executes it. 

If the final execution looks like this:

cursor.execute("select * from t where s like '%whatever%' and k =
%(something)s", {'something' : 'else'})

psycopg2 throws an exception

<type 'exceptions.TypeError'>: 'dict' object is unindexable

here, the script added the k = %(something)s part, and the parameters.

By trial and error, and partial reading of the source code, I discover
that doing something like:

cursor.execute("select * from t where s like '%%whatever%%' and k =
%(something)s", {'something' : 'else'})

works fine, even if it is ugly.  Also, if the parameters are not passed
in, then it doesn't convert the doubles to a single:

ie
>>> cursor.mogrify("select * from t where s like '%%whatever%%'")
"select * from t where s like '%%whatever%%'"

This isn't too much of a problem since it will match whatever was
matched before, but it is even uglier.

Is there a way to force psycopg2 to only take into account pyformat
variables and to leave the other %'s alone?  If not, would you accept a
patch to do this?
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.