TypeError: not enough arguments for format string for mysqldb
TypeError: not enough arguments for format string for mysqldb
我正在尝试将大量 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
executemany()
应该传递一个元组序列作为第二个参数(参见 the docs)。以下应该适合您:
cursor.executemany("INSERT INTO schema.table (Account_Number, Sales_Total) "
"VALUES(%s, %s)", [('Account 1', 2)])
奇怪错误消息的解释:在您的代码中,您传递的是单个元组,它也是一个序列,因此 executemany()
尝试仅使用 'Account 1'
来格式化查询,因此抱怨它没有足够的参数。
编辑:
P.S。字符串也是(字符的)序列,但特别是字符串格式将它们视为单个值而不是字符序列。否则,原始代码会产生一个错误,抱怨参数太多而不是太少...
我正在尝试将大量 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
executemany()
应该传递一个元组序列作为第二个参数(参见 the docs)。以下应该适合您:
cursor.executemany("INSERT INTO schema.table (Account_Number, Sales_Total) "
"VALUES(%s, %s)", [('Account 1', 2)])
奇怪错误消息的解释:在您的代码中,您传递的是单个元组,它也是一个序列,因此 executemany()
尝试仅使用 'Account 1'
来格式化查询,因此抱怨它没有足够的参数。
编辑:
P.S。字符串也是(字符的)序列,但特别是字符串格式将它们视为单个值而不是字符序列。否则,原始代码会产生一个错误,抱怨参数太多而不是太少...