[英]upsert into Postgres dealing with quotations
I'm trying to write a function that upserts a list of dicts into postgres, but I'm having difficulty dealing with quotation marks. 我正在尝试编写一个将字典列表向上插入postgres的函数,但是我很难处理引号。
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. 任何帮助是极大的赞赏。
I was able to solve it with the below changes. 我可以通过以下更改来解决它。
def upsert(cur, record, table, on_key):
"""creates the sql insert statement"""
cols = list(record.keys())
cols_set = ','.join([str(x) for x in cols])
vals = [record[x] for x in cols]
vals_str_list = ["%s"] * len(vals)
vals_str = ", ".join(vals_str_list)
cols.remove(on_key)
update_set = ','.join([str(x) for x in cols])
exclude_set = ','.join(['EXCLUDED.' + str(x) for x in cols])
cur.execute("""INSERT INTO {table} ({cols})
VALUES ({vals_str})
ON CONFLICT ({on_key})
DO UPDATE SET ({update_set}) = ({exclude_set})""".format(table=table, cols=cols_set, vals_str=vals_str, on_key=on_key, update_set=update_set, exclude_set=exclude_set),
vals)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.