TypeError:mysqldb格式字符串的参数不足

时间:2016-01-12 05:35:35

标签: python mysql python-2.7 mysql-python

我正在尝试将大量csv数据推送到mysql数据库并继续出错,因此我将插入简化为以下内容。我在这里看到了一些类似的问题,但我还没有能够让这些问题适合我。我错过了什么?它还应该注意到,当我尝试它时,它打印得很好。

import MySQLdb as mdb
con = mdb.connect(host='', port=3306, user='', passwd='', db='')


cursor = con.cursor()
cursor.executemany("INSERT INTO schema.table (Account_Number, Sales_Total) "
                       "VALUES(%s, %s)", ('Account 1', 2))

错误:

TypeError: not enough arguments for format string

1 个答案:

答案 0 :(得分:1)

executemany()应该作为第二个参数传递一系列元组(参见the docs)。以下内容适用于您:

cursor.executemany("INSERT INTO schema.table (Account_Number, Sales_Total) "
                   "VALUES(%s, %s)", [('Account 1', 2)])

奇怪的错误消息的解释:在您的代码中,您传递的是一个单元组,这也是一个序列,因此executemany()尝试使用'Account 1'格式化查询,因此会抱怨它没有足够的论据。

编辑:

P.S。字符串也是(字符)序列,但具体的字符串格式将它们视为单个值而不是字符序列。否则,原始代码会产生一个错误,抱怨太多的参数而不是太少......