python mysql多行插入失败

标签 python mysql sql-insert

我只是想将数据插入到我为测试而创建的小型 mysql (innodb) 表中。单行插入有效:

cursor.execute("insert into testy (one, two) values (%s,%s)", ('00','01'))

但是多行插入失败:

cursor.execute("insert into testy (one, two) values (%s,%s)", [('00','01'),('10','11'),('20','21')])

并给出以下错误:

TypeError - not all arguments converted during string formatting

traceback:
  File "./testy.py", line 20, in <module>
    cursor.execute("insert into testy (one, two) values (%s,%s)", [('00','01'),('10','11'),('20','21')])
  File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 159, in execute
    query = query % db.literal(args)

我需要进行多行插入,因为它比循环单行插入要快得多。我做错了什么?

最佳答案

使用executemany method而不是 execute :

cursor.executemany("insert into testy (one, two) values (%s,%s)",
                   [('00','01'),('10','11'),('20','21')])

关于python mysql多行插入失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20297819/

相关文章:

python - Matplotlib 表格格式化

python - 关于lambda的另一个问题, "k"在这里是什么意思?

PHP JSON数组解码并将数据存储到mysql

mysql - : 30 separate queries, 或 30 OR 条件哪个更快?

python - 如何将多行信息插入到mysql数据库的不同行中?

python - 使用pandas,如何加速需要使用前几行计算值的迭代?

python - 如何用 numpy 的行平均值替换丢失/屏蔽的数据

php - 对于 PHP,这种引用数据库信息的方法安全吗?

php - 使用多个 SQL 调用插入大型 CSV 太慢

tsql - T-SQL 删除插入的记录