Re: psycopg2 gets confused by % characters and strict pyformat mode
"James Henstridge" <[email protected]>
| Newsgroups | gmane.comp.python.db.psycopg.devel |
|---|---|
| Message-ID | <[email protected]> |
On Wed, Sep 10, 2008 at 5:31 PM, Federico Di Gregorio <fog-NGVKUo/i/[email protected]> wrote: > 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. Isn't that likely to break existing applications that depend on the current behaviour? From what I can see, the current behaviour when parameters are passed matches the DB-API specification. It is debatable whether the behaviour when no parameters are passed is correct (should it parse the '%' signs in that case?), but this proposal seem to move things further away. James.