【问题标题】:Error with update() with web.py使用 web.py 的 update() 出错
【发布时间】:2012-11-23 00:49:20
【问题描述】:

我正在尝试通过带有 web.py 的表单 POST 更新数据库行,但我收到了 MySQL 语法错误。我一般是 Python 新手,所以请原谅我的新手问题。这是我正在运行的代码:

   def Update(webInput):
      db.update('category', where='category_name=' + webInput.category_name)
      return;

我得到的错误是

ProgrammingError: (1064, "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 'WHERE category_name=' at line 1")

谢谢

【问题讨论】:

    标签: python python-2.7 web.py


    【解决方案1】:

    check the docs 获取 db.update。正确的调用是:

    db.update('category', where='category_name = $category_name', vars=webInput, **webInput)
    

    假设webInput 持有要设置的新数据。

    $category_name in where 子句从vars 中提取并进行了清理(即使您的代码有效,它也容易受到 sql 注入的攻击)。

    【讨论】:

    • 我觉得我很厚。我尝试了以下方法: db.update('category', where='categoryID= $category_id', vars=webInput) 我收到错误“您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“WHERE categoryID = '8'' 附近使用正确的语法”语法。
    • 更新查询应该更改值,您应该将数据库中必须更新的值作为关键字参数传递。 vars 仅用于格式化where 子句。
    【解决方案2】:

    您似乎忘记传递要用于更新“类别”的数据。

    试试:

    db.update('category', where='category_name=' + webInput.category_name, 'new data here')
    

    【讨论】:

    • 你不需要在web.py数据库where语句中的字符串周围加上引号?我刚刚开始了解 web.py
    • 新数据是一个名为“category_name”的表单字段。我该如何参考?我以为是通过 webInput.category_name
    猜你喜欢
    • 1970-01-01
    • 2012-12-16
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-17
    • 2018-11-13
    • 2019-10-23
    相关资源
    最近更新 更多