I'm writing a function to dynamically create insert/update/select queries for PostgreSQL database using psycopg2 library.
I've been trying to write the function injection-safe according to the recommendations provided by psycopg2 documentation - using sql.Sql method to compose the query correctly. All the parameters (table name, columns to insert, the values) are passed dynamically to the function:
def insert(table, columns: list, values: list):
query = sql.SQL('INSERT INTO {} ({}) VALUES ({});').format(
sql.Identifier(table),
sql.SQL(', ').join(map(sql.Identifier, columns)),
sql.SQL(', ').join(map(sql.Identifier, values))
)
cursor = foo_connection.cursor()
cursor.execute(query)
When I'm trying to test the function:
insert('test_table', ['col1', 'col2', 'col3'], ['1', '2', '3'])
I get the following error:
psycopg2.errors.UndefinedColumn: column "1" does not exist
LINE 1: ...e" ("col1", "col2", "col3") VALUES ("1", "2", ...
I don't undesrtand the error since technically it is not even a column, it is a value to be inserted.
I thought the query was composed incorrectly but the result of print(query.as_string(foo_connection)) shows that it seems to be correct:
INSERT INTO "test_table" ("col1", "col2", "col3") VALUES ("1", "2", "3");
The official documentation does not cover that situation. Googling did not give me the answers either.
So, the question are:
- What am I doing wrong here?
- How to make this code work?
UndefinedColumn: column "1" does not existseems to suggest that you are trying to insert a value into a column named "1", which isn't an actual column in your table. - ron_g