【问题标题】:Python MYSQL update statementPython MYSQL 更新语句
【发布时间】:2010-11-21 09:33:56
【问题描述】:

我正在尝试使这个 Python MYSQL 更新语句正确(带变量):

cursor.execute ("UPDATE tblTableName SET Year=%s" % Year ", Month=%s" % Month ", Day=%s" % Day ", Hour=%s" % Hour ", Minute=%s" Minute "WHERE Server=%s " % ServerID)   

任何想法我哪里出错了?

【问题讨论】:

    标签: python mysql mysql-python sql-parametrized-query


    【解决方案1】:

    should be:

    cursor.execute ("""
       UPDATE tblTableName
       SET Year=%s, Month=%s, Day=%s, Hour=%s, Minute=%s
       WHERE Server=%s
    """, (Year, Month, Day, Hour, Minute, ServerID))
    

    也可以通过基本的字符串操作来做到这一点,

    cursor.execute ("UPDATE tblTableName SET Year=%s, Month=%s, Day=%s, Hour=%s, Minute=%s WHERE Server='%s' " % (Year, Month, Day, Hour, Minute, ServerID))
    

    但是this way is discouraged because it leaves you open for SQL Injection。因为以正确的方式tm 这样做非常容易(并且类似)。正确地做。

    唯一需要注意的是,一些数据库后端不遵循相同的字符串替换约定(想到 SQLite)。

    【讨论】:

    • Paolo 的回答更好。 stackoverflow.com/questions/1307378/…
    • 但是这个适用于每个后端。此版本不对输入进行任何验证,而 Paolo 的方式将确保对变量内容进行转义。
    • 不要不要这样做。您让自己对 SQL 注入攻击敞开大门。 Paulo 有正确的答案,因为它确保在将值传递给 db 之前对其进行正确转义。
    • 抄袭 Paolo 的答案,因为我无法删除已接受的答案。
    • @dios231 三引号允许您编写多行字符串。如果您要打印上述文本的repr,它将是:"\n UPDATE tblTableName\n SET Year=%s, Month=%s, Day=%s, Hour=%s, Minute=%s\n WHERE Server=%s\n"
    【解决方案2】:

    你的语法全错了:

    cursor.execute ("""
       UPDATE tblTableName
       SET Year=%s, Month=%s, Day=%s, Hour=%s, Minute=%s
       WHERE Server=%s
    """, (Year, Month, Day, Hour, Minute, ServerID))
    

    更多信息,read the documentation

    【讨论】:

    • 如果我改用 """ update tblname ...""".format(var1, var2) 有关系吗?
    • 有没有办法找到rows affected消息,我们可以在MySQL workbench看到消息
    【解决方案3】:

    这是正确的做法:

    import MySQLdb
    
    if __name__ == '__main__':
        connect = MySQLdb.connect(host="localhost", port=3306,
                                  user="xxx", passwd="xxx", db='xxx', charset='utf8')
    
        cursor = connect.cursor()
    
        cursor.execute("""
           UPDATE tblTableName
           SET Year=%s, Month=%s, Day=%s, Hour=%s, Minute=%s
           WHERE Server=%s
        """, (Year, Month, Day, Hour, Minute, ServerID))
    
        connect.commit()
        connect.close()
    

    附:别忘了connect.commit(),不然就不行了

    【讨论】:

    • 是的,需要提交。在我没有提交的情况下,它锁定了整个表,然后肯定没有查询在该表上工作。
    【解决方案4】:

    出于某种原因,它们都不适合我。

    我发现由于某种原因 python 不读取 %s。所以在你的 SQL 代码中使用 (?) 而不是 %S。

    最后这对我有用。

       cursor.execute ("update tablename set columnName = (?) where ID = (?) ",("test4","4"))
       connect.commit()
    

    【讨论】:

    • 你的答案是错误的。您必须提交连接,而不是光标。 connection.commit() 就可以了。
    【解决方案5】:

    @Esteban Küber 绝对正确。

    对于像我这样该死的初学者来说,也许是一个额外的提示。 如果你用 %s 指定变量,你必须遵循这个原则 对于每个输入值,这意味着对于 SET 变量以及对于 WHERE 变量。

    否则,您将不得不面对终止消息,例如 '您的 SQL 语法有错误;查看与您的 MySQL 服务器版本相对应的手册,了解在 '%s WHERE' 附近使用的正确语法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-03
      • 2012-08-29
      • 2016-09-03
      • 2017-09-05
      • 2020-10-22
      • 1970-01-01
      相关资源
      最近更新 更多