gpt4 book ai didi

python - 类型错误 : not enough arguments for format string for mysqldb

转载 作者:行者123 更新时间:2023-11-28 23:39:39 28 4
gpt4 key购买 nike

我正在尝试将大量 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 格式化查询',因此提示它没有足够的参数。

编辑:

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

关于python - 类型错误 : not enough arguments for format string for mysqldb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34736256/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com