我有一个字典列表(大约20000个字典):
lista=[{'a':1, 'b':'hello','c':'home'},{'a':2, 'b':'sass','c':'qwwqw'};{'a':3, 'b':'ded','c':'eded'}]
"a"、"b"one_answers"c"是mysql表中的字段
在我的数据库中插入此列表的最佳方式是什么?我试过了,但不确定这是否是最好的选择。。。
for i in lista:
cursor.execute("insert....", i.value())
- 启动事务
- 执行一堆INSERT语句
- 提交事务
您应该能够对多行数据使用一个insert语句。我以前做过。以下是MySQL参考手册中的一部分。
INSERT statements that use VALUES syntax can insert multiple rows.
To do this, include multiple lists of column values, each enclosed
within parentheses and separated by commas. Example:
INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
这是该页面的链接。
https://dev.mysql.com/doc/refman/5.5/en/insert.html