gpt4 book ai didi

python - 将 python 用于 MySQL 的 INSERT

转载 作者:可可西里 更新时间:2023-11-01 08:34:41 24 4
gpt4 key购买 nike

我正在尝试使用以下命令插入到 MYSQL 数据库中:

add_contact = "INSERT INTO contacts (id, name, industry, phone, fax, url, pobox, emirate,ranking) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"

data_contact = (0, fields[2], fields[0], fields[5], fields[6], fields[1], fields[3], fields[4], float(totalhits))

cursor.execute(add_contact, data_contact)

我收到以下错误:

Traceback (most recent call last):
File "reputation.py", line 53, in <module>
cursor.execute(add_contact, data_contact)
File "/Library/Python/2.7/site-packages/mysql/connector/cursor.py", line 381, in execute
"Wrong number of arguments during string formatting")
mysql.connector.errors.ProgrammingError: Wrong number of arguments during string formatting

这让我怀疑我数到 9 的能力:-)

这段代码可能有什么问题?

更新:

更改为

add_contact = ("INSERT INTO contacts (id, name, industry, phone, fax, url, pobox, emirate,ranking)" “值(%s、%s、%s、%s、%s、%s、%s、%s、%s)”)

加上

cnx.commit()

解决了!

谢谢!

最佳答案

我认为你应该使用 % 而不是 ?:

add_contact = "INSERT INTO contacts (id, name, industry, phone, fax, url, pobox, emirate,ranking) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"

?sqlite3 中用作占位符。在 psycopg2MySQLdb 中,它是 %

关于python - 将 python 用于 MySQL 的 INSERT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16433833/

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