aiopg
aiopg copied to clipboard
How to handle table names in queries?
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.