Python类型元组无法转换MySQL
Python type tuple cannot be converted MySQL
我不太明白元组是如何工作的。但我所知道的是它们看起来像这样。
(1, variable)
但我一直收到错误消息:
Python type tuple cannot be converted
有人可以帮忙吗这是我的代码:
@client.command()
async def buy(ctx, item: str):
USER_ID = ctx.message.author.id
write_log("Buy command requested")
#write_log("Sending GET request to Cosmos API...")
try:
SQL.execute("SELECT price FROM shop WHERE itemname = %s", (item,))
price = SQL.fetchone()
SQL.execute("SELECT balance FROM Accounts WHERE user_id = %s", (USER_ID,))
SQL.execute("UPDATE Accounts SET balance = balance - %s WHERE user_id = %s", (price, USER_ID))
db.commit()
await ctx.send(f"Successfully bought **{item}** for **{price} Rollars**.")
except Exception as ex:
write_log('ERROR')
full_traceback = traceback.format_exc()
write_log(f'Errored whilst sending ctx(embed), (about): {ex} {full_traceback}')
完整回溯:
Traceback (most recent call last):
File "Bot.py", line 316, in buy
SQL.execute("UPDATE Accounts SET balance = balance - %s WHERE user_id = %s", (price, USER_ID))
File "/home/vihanga/lib/python3.8/site-packages/mysql/connector/cursor_cext.py", line 248, in execute
prepared = self._cnx.prepare_for_mysql(params)
File "/home/vihanga/lib/python3.8/site-packages/mysql/connector/connection_cext.py", line 626, in prepare_for_mysql
result = self._cmysql.convert_to_mysql(*params)
_mysql_connector.MySQLInterfaceError: Python type tuple cannot be converted
如果你能向我解释它是如何工作的并可能帮助我解决错误,我将不胜感激。
fetchone() 函数 returns 将结果作为元组。因此,当您使用 fetchone 时,存储在 price 变量中的值是一个如下所示的元组:(100,)
。
然后,当您执行更新查询时,您正在传递整个元组,就像您在另一个元组中收到它一样。例如,如果价格值返回为 100
,那么下面是您传递给执行函数的内容。
SQL.execute("UPDATE ..... ", ((100,), USER_ID))
所以你的参数的第一个元素是一个元组而不是一个值,你得到那个错误是因为数据库认为那里有一些它可以使用的值,但是因为它找到了一个元组而不是第一个元素你得到那个错误。
你应该做的只是传递一个整数值作为价格参数,所以它看起来像:SQL.execute("UPDATE ..... ", (100, USER_ID))
为此,您可以通过在元组前面放置一个 * 来解压缩元组,或者从中获取第一个值,例如 price[0]
.
我不太明白元组是如何工作的。但我所知道的是它们看起来像这样。
(1, variable)
但我一直收到错误消息:
Python type tuple cannot be converted
有人可以帮忙吗这是我的代码:
@client.command()
async def buy(ctx, item: str):
USER_ID = ctx.message.author.id
write_log("Buy command requested")
#write_log("Sending GET request to Cosmos API...")
try:
SQL.execute("SELECT price FROM shop WHERE itemname = %s", (item,))
price = SQL.fetchone()
SQL.execute("SELECT balance FROM Accounts WHERE user_id = %s", (USER_ID,))
SQL.execute("UPDATE Accounts SET balance = balance - %s WHERE user_id = %s", (price, USER_ID))
db.commit()
await ctx.send(f"Successfully bought **{item}** for **{price} Rollars**.")
except Exception as ex:
write_log('ERROR')
full_traceback = traceback.format_exc()
write_log(f'Errored whilst sending ctx(embed), (about): {ex} {full_traceback}')
完整回溯:
Traceback (most recent call last):
File "Bot.py", line 316, in buy
SQL.execute("UPDATE Accounts SET balance = balance - %s WHERE user_id = %s", (price, USER_ID))
File "/home/vihanga/lib/python3.8/site-packages/mysql/connector/cursor_cext.py", line 248, in execute
prepared = self._cnx.prepare_for_mysql(params)
File "/home/vihanga/lib/python3.8/site-packages/mysql/connector/connection_cext.py", line 626, in prepare_for_mysql
result = self._cmysql.convert_to_mysql(*params)
_mysql_connector.MySQLInterfaceError: Python type tuple cannot be converted
如果你能向我解释它是如何工作的并可能帮助我解决错误,我将不胜感激。
fetchone() 函数 returns 将结果作为元组。因此,当您使用 fetchone 时,存储在 price 变量中的值是一个如下所示的元组:(100,)
。
然后,当您执行更新查询时,您正在传递整个元组,就像您在另一个元组中收到它一样。例如,如果价格值返回为 100
,那么下面是您传递给执行函数的内容。
SQL.execute("UPDATE ..... ", ((100,), USER_ID))
所以你的参数的第一个元素是一个元组而不是一个值,你得到那个错误是因为数据库认为那里有一些它可以使用的值,但是因为它找到了一个元组而不是第一个元素你得到那个错误。
你应该做的只是传递一个整数值作为价格参数,所以它看起来像:SQL.execute("UPDATE ..... ", (100, USER_ID))
为此,您可以通过在元组前面放置一个 * 来解压缩元组,或者从中获取第一个值,例如 price[0]
.