aiopg icon indicating copy to clipboard operation
aiopg copied to clipboard

How to handle table names in queries?

Open cdvv7788 opened this issue 4 years ago • 0 comments

psycopg has a documentation section explaining why you cannot use %s for table names (quoting will cause syntax errors).

In aiopg examples, there is nothing that shows how this could be done.

I was able to run it by doing something like this:

from psycopg2 import sql
query = sql.SQL("select * from {}").format(sql.Identifier(table_name))
query = query.as_string(conn._connection._conn) # Use the wrapped psycopg connection

This works, but is there a less hacky way to achieve this? I could not find it in the documentation.

cdvv7788 avatar Nov 24 '20 20:11 cdvv7788