python mysql多行插入失败

时间:2013-11-30 07:32:56

标签: 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)

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

1 个答案:

答案 0 :(得分:0)

使用executemany method代替execute

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