pymysql 的转义处理
问题描述
写爬虫时遇到了字符串中同时含有单双引号,在往数据库中写数据时出现语法错误
解决方案
查阅文档后发现pymysql有个
# 代码实例
db = pymysql("localhost","user","password",charset="utf8")
cursor = db.cursor()
exp = db.escape(str)
sql = "... where id = %s" % exp #%s无需引号
with db.cursor() as cursor:
try:
cursor.execute(sql)
db.commit()
except Exception as e:
print(e)
db.rollback()
db.close()

浙公网安备 33010602011771号