I am trying to safely pass in a query string and parameters to psycopg2, but am having trouble with formatting lists into the query. In the first file, I have a list of columns and values to pass through to another function that executes the query.
columns = ['id', 'name', 'address']
values = ['1234', 'john', '123 road']
query = """INSERT INTO table %s VALUES (%s)"""
parameters = (tuple(columns), tuple(values))
res = update(query, parameters)
In the second file I take the query string and parameter and execute them:
def update(query_string: str, query_parameters: tuple):
with db.transaction() as txn:
txn.execute(query_string, query_parameter)
But I get an error saying:
LINE 1: INSERT INTO table ('id', 'name', 'address')...
^
Syntax Error
What would be the correct way to pass in the column and values list into the query string?