带有特殊字符的mysql::connector/python update语句不起作用

pnwntuvh  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(326)

我正在尝试使用selenium和beautifulsoup抓取网页,然后将结果存储到mysql。结果包含许多特殊字符,如;'“=(因为它正在解析html)
我遵循了mysql连接器/python和insert的工作原理,但是更新了。
如果我选择我的表,我可以看到这样的东西

<div style="width:80px; float: ~~~ ... </div>

这意味着目标html元素已成功存储。这是我的插入和更新。


# after import and configs

soup = BeautifulSoup(innerHTML, "html.parser")
text = soup.prettify()
print(text)

# this INSERT works fine

# insert = ("INSERT INTO g_cache (a_col, cache_contents) VALUES(%s, %s)")

# data_row = ('aaa', text)

# cursor.execute(insert, data_row)

# Trying to UPDATE already inserted row. Not working

# I executed and commented above INSERT before run the following UPDATE

update = ("""UPDATE g_cache SET cache_contents =%s WHERE id=21""")
data_col = (text)
cursor.execute(update, data_col)

# If I use one complete string, it works though. For example

# cursor.execute("""UPDATE g_cache SET cache_contents ='111' WHERE id=21""")

cnx.commit()

错误消息为
mysql.connector.errors.programmingerror:1064(42000):您的sql语法有错误;请查看与您的mysql服务器版本对应的手册,以获取在第1行的“%s where id=21”附近使用的正确语法
我刚刚介绍了mysql::connector/pythonapi。
我不明白为什么insert可以工作,而update不能,使用相同的字符串。
有人知道吗??

wxclj1h5

wxclj1h51#

参数必须作为Map(即dict)或元组传递。在只传递一个参数时省略尾随逗号会将其视为标量,因此必须按如下方式传递参数:

data_col = (text,)

文件还提到,在一个突出的说明。

相关问题