python - 警告 : 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

标签 python mysql xml

我正在将解析器 xml 编码到 mysql

错误的全名:

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 'CHA3871376-ABZ-1', '\xd0\x91\xd1\x80\xd0\xbe\xd0\xbd\xd0\xb7\xd0\xbe\xd0\xb2\xd0\xbe\xd0\xb5 \xd0\xbc\xd0\xb5\xd1\x82\xd0\xb0\xd0\xbb\xd0\xbb\xd0\xb8\xd1\x87\xd0\xb5\xd1\x81\xd0\xba\xd0\xbe\xd0\xb5 \xd0\xba\xd0\xbe\xd0\xbb\xd1\x8c\xd1\x86\xd0\xbe ' at line 1")

我有 xml 文件

<model>CHA3871376-ABZ-3</model> 

我认为麻烦在于编码 但我正在努力

offer_model = item.getElementsByTagName("model")[0].firstChild.nodeValue.decode('utf-8')
sqlfillOffers = "INSERT INTO offers (offer_id, url, price, currency_id, typePrefix, vendor, model, description) VALUES ('"+str(offer_id) + "', '" + str(offer_url) + "', '" + str(offer_price) + "', '" + str(offer_CurrId) + "', '"+str(offer_typePrefix)+"', '"+str(offer_vendor)+"', '"+str(offer_model)+"', '"+str(offer_description)+"');"

出了什么问题?

最佳答案

不要使用字符串插值来构建 SQL;不要您将引入错误并使自己容易受到 SQL 注入(inject)攻击。

改用 SQL 参数,并让数据库适配器为您处理转义值:

sqlfillOffers = (
    "INSERT INTO offers (offer_id, url, price, currency_id, typePrefix, vendor, model, description) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, )"
)

cursor.execute(
    sqlfilOffers,
    (offer_id, offer_url, offer_price, offer_CurrId, offer_typePrefix, offer_vendor, offer_model, offer_description)
)

关于python - 警告 : 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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19343284/

相关文章:

Python笛卡尔积和条件?

python - 如何在 for 循环中分配新变量 - pandas dataframe 变量名称分配

sql - 表示数据库中的时间表

mysql - 根据其他行的值计算百分比

xml - 在 Clojure 中如何在集合上应用函数

python - 正则表达式:排除基于非捕获组的结果?

mysql - 将 mysqldump 数据填充到表 : only for rows which exist in a given database

c# - 在 C# 中对 XPathNodeIterator 的内容进行排序

xml - 用于修改 JetBrains 项目中 .idea 文件夹中 XML 的命令行工具

python - 通过递归传递 lambda