Python MySQL连接器选择变量
我有一个小小的疑问:Python MySQL连接器选择变量,python,mysql,sql,Python,Mysql,Sql,我有一个小小的疑问: 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 MySQ
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,)
试试sql=“从id=?”的用户选择id,密码”