【问题标题】:Unsure of the correct syntax regarding passing multiple variables to an sql query in Python [duplicate]不确定有关将多个变量传递给 Python 中的 sql 查询的正确语法 [重复]
【发布时间】:2018-11-28 20:58:46
【问题描述】:

我正在尝试编写 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 登录的用户的密码,他们的用户名(在本例中为电子邮件地址)。

任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: python mysql sql flask


    【解决方案1】:

    您需要将它们作为要插入到执行语句中的值的元组来提供。

    语法如下: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 或使用原始独库。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-25
      • 2019-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      • 2018-09-23
      • 2019-07-11
      相关资源
      最近更新 更多