不确定将多个变量传递给 Python 中的 sql 查询的正确语法
Unsure of the correct syntax regarding passing multiple variables to an sql query in Python
我正在尝试编写 SQL 查询来更新当前登录到 Web 应用程序的用户的密码。我正在使用会话 ID 来识别要为其更新密码的特定用户。但是,我不确定如何为查询编写正确的语法
这里是有问题的查询:
cursor.execute("UPDATE user SET password = %s WHERE email = ?", [confirm_password], session['email'])
结果产生的错误:
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 '?' at line 1
我只想更新使用用户名(在本例中为电子邮件地址)的会话 ID 登录的用户的密码。
如有任何帮助,我们将不胜感激。谢谢
您需要将它们作为要插入到执行语句中的值的元组提供。
语法如下:cursor.execute(operation, params=None, multi=False)
operation
是您的查询字符串
params
是一个包含所有参数的元组
cursor.execute("UPDATE user SET password = %s WHERE email = %s",
(confirm_password, session['email']) )
你可能还应该使用 % 两次..
见connector-python-api-mysqlcursor-execute
如有疑问,我在这里查找语法:http://bobby-tables.com/python 或使用原始 doku。
我正在尝试编写 SQL 查询来更新当前登录到 Web 应用程序的用户的密码。我正在使用会话 ID 来识别要为其更新密码的特定用户。但是,我不确定如何为查询编写正确的语法
这里是有问题的查询:
cursor.execute("UPDATE user SET password = %s WHERE email = ?", [confirm_password], session['email'])
结果产生的错误:
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 '?' at line 1
我只想更新使用用户名(在本例中为电子邮件地址)的会话 ID 登录的用户的密码。
如有任何帮助,我们将不胜感激。谢谢
您需要将它们作为要插入到执行语句中的值的元组提供。
语法如下:cursor.execute(operation, params=None, multi=False)
operation
是您的查询字符串params
是一个包含所有参数的元组
cursor.execute("UPDATE user SET password = %s WHERE email = %s",
(confirm_password, session['email']) )
你可能还应该使用 % 两次..
见connector-python-api-mysqlcursor-execute
如有疑问,我在这里查找语法:http://bobby-tables.com/python 或使用原始 doku。