Re: psycopg2 gets confused by % characters and strict pyformat mode

Federico Di Gregorio <fog-NGVKUo/i/[email protected]>
Newsgroups gmane.comp.python.db.psycopg.devel
Organization initd.org
Message-ID <[email protected]>
Il giorno mer, 10/09/2008 alle 18.13 +1000, Alejandro Dubrovsky ha
scritto:
> 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?

Currently there is no way. A patch would be welcome.

federico

-- 
Federico Di Gregorio                         http://people.initd.org/fog
DISCLAIMER. If I receive a message from you, you are agreeing that:
 1. I am by definition, "the intended recipient".
 2. All information in the email is mine to do with as I see fit and
 make such financial profit, political mileage, or good joke as it lends
 itself to. In particular, I may quote it on USENET or the WWW.
 3. I may take the contents as representing the views of your company.
 4. This overrides any disclaimer or statement of confidentiality that
 may be included on your message.

_______________________________________________
Psycopg mailing list
Psycopg-IAPFreCvJWPBWskQ1e/[email protected]
http://lists.initd.org/mailman/listinfo/psycopg
signature.asc (application/pgp-signature, 197 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkjHk+cACgkQvcCgrgZGjetl0gCffdft34Wc4r1EwMczabtad2+/
ZZMAnAnWIEGqAY5kbOw1zyAWz56omRV9
=wbDr
-----END PGP SIGNATURE-----
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.