TypeError在PythonmySql中转换期间未转换所有参数

2024-09-20 05:35:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个问题,为什么我会犯这个错误。 我尝试将python脚本中的一些数据插入mysql

这是我的代码:

queryinsertStatus = "INSERT INTO general (status) VALUES (%s, )"
onlinestatus = (y["msg"])
print(onlinestatus)
cursor.execute(queryinsertStatus, onlinestatus)

但结果我得到了:

API online

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/MySQLdb/cursors.py", line 204, in execute
    query = query % args
TypeError: not all arguments converted during bytes formatting

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "trading.py", line 30, in <module>
    cursor.execute(queryinsertStatus, onlinestatus)
  File "/usr/lib/python3/dist-packages/MySQLdb/cursors.py", line 206, in execute
    raise ProgrammingError(str(m))
MySQLdb._exceptions.ProgrammingError: not all arguments converted during bytes formatting

我错了什么?onlinestatus包含一个“API online”

mySQL属于tinytext类型

希望有人能帮助我


Tags: inpyapimostexecutelinecallcursor
2条回答

讽刺的是,SQL(VALUES (%s))中不应该有逗号,但参数中确实需要逗号。execute方法希望得到一个元组(y["msg"])只是一个字符串。要使其成为一个单元素元组,请使用(y["msg"],)

这只是Python的一个奇怪的角落

SQL语句本身似乎有问题,即“,”似乎不属于VALUES部分。或许可以这样尝试:

queryinsertStatus = "INSERT INTO general (status) VALUES (%s)"

相关问题 更多 >