pymysql 引号转义处理方法

pymysql直接插入带有双引号或单引号的字符串,报错:

pymysql.err.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 '

 

解决方法:

使用pymysql.escape_string(str) 转义:
escape_string(record["content"])
# v1.0.0及以上
from pymysql.converters import escape_string

# v0.10.1及以下
from pymysql import escape_string
#data为含有单引号或者双引号的字符串
data = escape_string(data)

 

posted @ 2022-11-09 11:36  黑山老道  阅读(480)  评论(0编辑  收藏  举报