【问题标题】:TypeError: not enough arguments for format string for mysqldbTypeError:没有足够的参数用于 mysqldb 的格式字符串
【发布时间】:2016-04-16 15:08:33
【问题描述】:

我正在尝试将大量 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

【问题讨论】:

标签: python mysql python-2.7 mysql-python


【解决方案1】:

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

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

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

编辑:

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

【讨论】:

    猜你喜欢
    • 2012-07-09
    • 2014-04-07
    • 1970-01-01
    • 2020-04-12
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    • 2020-10-17
    • 2012-06-11
    相关资源
    最近更新 更多