我正在尝试使用 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/