psycopg2 / psycopg2.ProgrammingError: syntax error at or near "E'mytable'"
MRAB
python at mrabarnett.plus.com
Fri Apr 2 21:27:31 EDT 2010
mrdrew wrote:
> Hey all,
>
> Right now I'm completely unable to pass parameters to queries under
> any circumstances. I've got a fairly trivial query as a test...
>
> c.execute('SELECT * FROM %(table_name)s LIMIT 1',
> {'table_name':"mytable"})
>
> It fails, giving the error message...
>
> Traceback (most recent call last):
> File "test.py", line 7, in <module>
> c.execute('SELECT * FROM %(table_name)s LIMIT 1',
> {'table_name':"mytable"})
> psycopg2.ProgrammingError: syntax error at or near "E'mytable'"
> LINE 1: SELECT * FROM E'mytable' LIMIT 1
>
> This may be similar to the problem that ASh had (http://
> groups.google.com/group/comp.lang.python/browse_thread/thread/
> 7463ded0971425f8/538e60ba0ccf2ad3?#538e60ba0ccf2ad3)
>
> I'd really appreciate any ideas. At the moment, I'm stuck
> concatenating strings and hoping for the best.
I think that you're confusing Python's string formatting with SQL
placeholders.
The "%(table_name)s" works only with Python's '%' operator. You should
use only the "%s" form (or possibly "?", I'm not sure which!) in the
template string and pass the parameters in a tuple (maybe a list will
also work) when calling .execute().
More information about the Python-list
mailing list