gpt4 book ai didi

Python MySQL 连接器选择变量

转载 作者:行者123 更新时间:2023-11-29 11:55:57 25 4
gpt4 key购买 nike

我有一个小问题:

id = 'TESTID'
sql = "SELECT ID,PASSWORD FROM USERS WHERE ID = %s"
cursor.execute(sql,(id))

我遇到了错误:

mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TESTID''' at line 1

我知道这是关于双引号的事情。我还有多个其他查询可以完美运行,但它们有大约 3 个参数

示例:

id = 'TESTID'
GR = 'TEST'
name = 'HELLO'
last_name = 'WORLD'
sql = "INSERT INTO USERS (ID,GR,name,last_name) VALUES (%s,%s,%s,%s)"
cursor.execute(sql,(id,gr,name,last_name))

这个在开头没有 3 个双引号,在结尾没有另外 3 个双引号,并且运行完美,所以我不知道现在该怎么办。

谢谢你。

最佳答案

在 Python 中您应该记住的一件事是 (7)7 相同。对于长度为 1 的元组,您必须说 (7,) (注意重要的尾随逗号)。

所以改变这一行:cursor.execute(sql,(id))cursor.execute(sql,(id,))

关于Python MySQL 连接器选择变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33193809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com