python - 将 python 用于 MySQL 的 INSERT

标签 python mysql

我正在尝试使用以下命令插入到 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/

相关文章:

python - 优化遍历 numpy 数组

python - Python 中的深度学习 - 随机梯度下降 - 分解代码

mysql - 如何将.sql.bz2文件中的数据导入到hdfs?

python - 找不到模块错误(imutils.paths)

Python - Matplotlib 使用 pandas 数据框时绘制不正确的图形

python - 在 python 中将多个列表写入文本文件 - 2.7

mysql - 如何在 jOOQ 代码生成中将 mysql varbinary 映射到 String

mysql - 使用 sql 获取 AUTO_INCRMENT VALUE,而不使用 max(id) 或先前的 id 或行计数

mysql join,替换逗号分隔的字符串

php - where没有按预期工作