【发布时间】:2019-02-22 18:03:55
【问题描述】:
我有这样的数据元组列表:
list1 = [(1100, 'abc', '{"1209": "Y", "1210": "Y"}'), (1100, 'abc', None)]
def insert_sample_data(col_val):
cur = self.con.cursor()
sql = """insert into sampletable values {}""".format(col_val)
cur.execute(sql)
self.con.commit()
cur.close()
values1 = ', '.join(map(str, list1)) #bulk insert
insert_sample_data(values1)
表结构: ssid int,名称 varchar,规则 jsonb
当我尝试插入数据但它抛出一个错误说“插入列“无”不存在”。我们如何将数据加载到“Null”或“None”的表中?
我查看了这个解决方案,但在这种情况下它没有帮助 How to insert 'NULL' values into PostgreSQL database using Python?
【问题讨论】:
-
为什么链接问题的解决方案在这种情况下不起作用?因为批量插入?为此,如果速度对您很重要,您应该使用
executemany或execute_batch。
标签: python python-3.x postgresql