【问题标题】:Update rows in Mysql [closed]更新Mysql中的行[关闭]
【发布时间】:2013-05-27 09:53:00
【问题描述】:

我正在尝试更新 MySQL DB 中的数据,但它抛出错误:

UPDATE scrapedDataTable  SET productDesc=, 
                            moreProductImages=full/545521_1363869251%20copy-500x500.jpg,full/545521_1363869251%20copy-275x275.jpg,full/545522_1363869309%20copy-500x500.jpg,full/545522_1363869309%20copy-74x74.jpg, ,productCategory=Clothing, 
                            productSubCategory=NA  WHERE Server=http://1click1call.com/Jeans-Shirts-Tshirts-Trousers/W-for-Woman-Kurta-54552


And error:
(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 'moreProductImages=full/545521_1363869251%20copy-500x500.jpg,full/545521_13638692' at line 1")
"(<class '_mysql_exceptions.ProgrammingError'>)"

我的表结构是:

+--------------------+---------------+------+-----+---------+-------+
| Field              | Type          | Null | Key | Default | Extra |
+--------------------+---------------+------+-----+---------+-------+
| productTitle       | varchar(100)  | NO   |     | NULL    |       |
| productSite        | varchar(1000) | NO   |     | NULL    |       |
| productDesc        | text          | YES  |     | NULL    |       |
| productURL         | varchar(325)  | NO   | PRI | NULL    |       |
| productMRP         | varchar(200)  | YES  |     | NULL    |       |
| productPrice       | varchar(200)  | YES  |     | NULL    |       |
| productCategory    | varchar(50)   | YES  |     | NULL    |       |
| productSubCategory | varchar(50)   | YES  |     | NULL    |       |
| imageURL           | text          | YES  |     | NULL    |       |
| moreProductImages  | text          | YES  |     | NULL    |       |
+--------------------+---------------+------+-----+---------+-------+

查询:

cursor.execute("UPDATE scrapedDataTable  SET productDesc=%s,moreProductImages='%s', ,productCategory=%s, productSubCategory=%s  WHERE productURL=%s" %(productDesc,image_paths,productCategory,productSubCategory,productURL))

谁能帮帮我...

【问题讨论】:

  • 看起来你的所有字符串文字周围都缺少引号
  • 如果我在值列表之前删除 %,我会收到此错误:'str' object is not callable "'str' object is not callable (&lt;type 'exceptions.TypeError'&gt;)"

标签: python mysql


【解决方案1】:

您的 SQL 应该是:

UPDATE scrapedDataTable  SET productDesc='', 
                            moreProductImages='full/545521_1363869251%20copy-500x500.jpg,full/545521_1363869251%20copy-275x275.jpg,full/545522_1363869309%20copy-500x500.jpg,full/545522_1363869309%20copy-74x74.jpg, ',productCategory='Clothing', 
                            productSubCategory='NA'  WHERE Server='http://1click1call.com/Jeans-Shirts-Tshirts-Trousers/W-for-Woman-Kurta-54552'

然后等待。您的主键是一个长 URL 字符串?真的吗 ?最好使用整数之类的短键来保持表的索引良好。

问题更新后更新:该行应该是:

cursor.execute("UPDATE scrapedDataTable  SET productDesc='%s',moreProductImages='%s', ,productCategory='%s', productSubCategory='%s'  WHERE productURL='%s'" %(productDesc,image_paths,productCategory,productSubCategory,productURL))

【讨论】:

  • 请先看看我知道引号丢失的问题,我没有故意这样做我只是替换变量的值......再看看我编辑过的问题......
  • 答案已更新。这就是为什么在添加到查询之前需要对输入进行清理的原因。答案仍然有效,可以帮助您解决问题。
  • 你能告诉我如何逃避's,因为如果我使用'%s',那么它只需要字符串到''s'并忽略休息......
  • 不确定您使用的是哪个 MySQL 库,但一般来说,有一个名为 mysqli_real_escape_string(用于 MySQLi)的函数来转义输入。检查您的 MySQL 库。
  • -1 表示不好的做法。由所有 Python SQL 库实现的 Python 数据库 API 使用参数化查询——这些手动转义参数都不是 PHP 中常见的废话。您需要在查询中删除 %ss 周围的单引号,而不是手动(并且容易注入)使用 % 运算符将参数格式化到查询中,而是将 % 替换为逗号 @987654328 @ 将参数作为第二个参数传递给execute()