【问题标题】:python mysql multi-row insert failpython mysql多行插入失败
【发布时间】:2013-11-30 07:32:56
【问题描述】:

我只是想将数据插入一个我为测试而制作的小型 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)

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

【问题讨论】:

    标签: python mysql sql-insert


    【解决方案1】:

    使用executemany method 代替execute:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-27
      • 2016-06-21
      • 2014-11-01
      • 2021-12-05
      相关资源
      最近更新 更多