python – mysql.connector – SQL语法中有错误;第1行’%s’附近

我正在引用
this page以便对我的数据库进行SELECT查询.但是,我收到此错误:

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 '%s' at line 1

以下是带有问题的代码部分:

import mysql.connector
cnx = mysql.connector.connect(user='xxxx', password='yyyy',
                              host='zzzz.us-west-2.rds.amazonaws.com',
                              database='iiii')
cursor = cnx.cursor()

# ...

givenUsername = 'testUser123'
checkUserAuthQuery = ("SELECT password FROM UserAuth WHERE username = %s")
userAuthInfo = (givenUsername)
cursor.execute(checkUserAuthQuery, userAuthInfo)

# ...

笔记:
– 使用%s执行INSERT查询时,它可以正常工作.
– 另外,用’testuser123’替换%s有效.

最佳答案 您缺少逗号以使userAuthInfo成为元组.将其更改为:

userAuthInfo = (givenUsername,)
点赞