Skip to content
Advertisement

psycopg2 not escaping quotation from tuple arguments? Using sql.SQL to format arguments

I’m trying to dynamically create a table in postgres with psycopg2. It seems when the arguments are passed, pyscopg2 is not escaping quotations and understanding my query. Code looks like this:

The error I’m getting:

What print(create_table_str.as_string(conn)) outputs:

Edit to show modified answer that works with no string concatenation

Thanks to Adrian for the help

Advertisement

Answer

sql.Identifier is for SQL objects only e.g. table/column names not the types: https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS

Try:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement