psycopg2.ProgrammingError: syntax error at or near "\"

Karsten Hilbert Karsten.Hilbert at gmx.net
Fri Oct 27 11:52:44 EDT 2017


On Fri, Oct 27, 2017 at 08:35:20AM -0700, maheshyadav1771 at gmail.com wrote:

> I am using 'psycopg2' library to access my PostgreSQL database from a python. I want to pass a variable input in psql query something like this :- 
> 
> psql>>\set my_user table1
> psql>>select * from :my_user limit 10;
> 
> So I am just running these sql commands and getting this error :-
> 
> >>> cur.execute("""\set my_user table1""")
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> psycopg2.ProgrammingError: syntax error at or near "\"
> LINE 1: \set my_user table1

\set is a psql specific pseudo command rather than SQL. Only
SQL code can be run through psycopg2.

You want to read up on query parameter handling in the
psycopg2 docs. Psycopg2 and (/usr/bin/)psql(.exe) are not the
same thing.

Karsten
-- 
GPG key ID E4071346 @ eu.pool.sks-keyservers.net
E167 67FD A291 2BEA 73BD  4537 78B9 A9F9 E407 1346



More information about the Python-list mailing list