我正在尝试使用以下命令插入到 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
中用作占位符。在 psycopg2
和 MySQLdb
中,它是 %
。
关于python - 将 python 用于 MySQL 的 INSERT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16433833/