python - python中的mysqldb错误

标签 python mysql mysql-python

我正在尝试使用 Python 将一个值插入到我的本地 MySQL 实例中,但我一直收到错误

这是我的代码:

con = mdb.connect('localhost', 'root', 'password', 'testDB')
cur = con.cursor()
cur.execute('''INSERT INTO testDB.testPython VALUES (%s)''',("HelloWorld"))
con.commit()

但我收到以下错误

TypeError: not all arguments converted during string formatting

有人知道为什么吗?

最佳答案

TypeError是一个基本的 Python 错误:

Raised when an operation or function is applied to an object of inappropriate type. The associated value is a string giving details about the type mismatch.

你的问题是你忘记在你的元组中放置一个逗号,没有它被认为是括号中的对象:你的单个字符串。

翻译解释:

In [1]: ('Hello')
Out[1]: 'Hello'
In [2]: ('Hello',)
Out[2]: ('Hello',)

像这样修复以解决您的问题:

cur.execute('INSERT INTO testDB.testPython VALUES (%s)', ("HelloWorld",))

关于python - python中的mysqldb错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26517461/

相关文章:

python-2.7 - 如何在 Ubuntu 20.04 (Focal Fossa) 中为 Python 2.7 安装 python-mysqldb?

Raspberry Pi 上的 Python/MySQLdb 问题

Python:更新元组列表......最快的方法

python - 如何使用python将端口转发到路由器

python - 在 SQLAlchemy 中以 dict 形式检索查询结果

php - 连接 MySQL 数据库中的两个表

python - 执行数学 sigma 和的最快、最有效和 pythonic 方法是什么?

python - 使用常规 python 脚本运行 Nose 测试

c# - 如何在 C# 中与本地数据库同步时避免远程数据库中的重复

mysql - PyMySQL callproc() 不执行 proc 中的语句