def write_to_db(self, result, table, primary_key=None):
if len(result) == 0:
return
result_string = ['(' + ','.join(['\'%s\'' % c for c in r]) + ')' for r in result]
result_string = ',\n'.join(result_string)
query = """
INSERT INTO %s VALUES
%s
""" % (table, result_string)
if primary_key is not None:
query += "\nON CONFLICT (%s) DO NOTHING\n" % primary_key
with connect(self.connection_string) as con:
con.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
with con.cursor() as cur:
cur.execute(query)
cur.close()
评论列表
文章目录