I'm trying to write a function that upserts a list of dicts into postgres, but I'm having difficulty dealing with quotation marks.
I have the two functions below, one to generate the insert statement and one to execute.
import psycopg2 as pg
import string
def create_insert_sql(record, table, on_key):
"""creates the sql insert statement"""
columns = list(record.keys())
insert_columns = ','.join([str(x) for x in columns])
columns.remove(on_key)
update_set = ','.join([str(x) for x in columns])
exclude_set = ','.join(['EXCLUDED.' + str(x) for x in columns])
values = ','.join(['\'' + str(x) + '\'' for x in record.values()])
insert_sql_statement = """
INSERT INTO {} ({})
VALUES ({})
ON CONFLICT ({})
DO UPDATE SET
({})
= ({}) ;
""".format(table, insert_columns, values, on_key, update_set, exclude_set)
return insert_sql_statement
def upsert_into_pg(dataset, table, on_key):
"""Given a list of dicts, upserts them into a table with on_key as conflict"""
conn = pg.connect(user=XXXX,
password=XXXX,
host='127.0.0.1',
port=3306,
database='XXXX')
cur = conn.cursor()
try:
for record in dataset:
cur.execute(create_insert_sql(record, table, on_key))
except Exception as e:
conn.rollback()
print(e)
else:
conn.commit()
print('upsert success')
finally:
cur.close()
conn.close()
An example of the error
test = [
{'a': 'structure', 'b': 'l\'iv,id', 'c': 'doll', 'd': '42'},
{'a': '1', 'b': 'shoe', 'c': 'broke', 'd': '42'},
{'a': 'abc', 'b': 'proc', 'c': 'moe', 'd': '42'}
]
upsert_into_pg(test, 'testing', 'a')
Returning
syntax error at or near "iv"
LINE 3: VALUES ('structure','l'iv,id','doll','42')
Any help is greatly appreciated.