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 '[{\‘enText\’: \ …

解决方法:
使用pymysql.escape_string(str) 转义:

articleDetail = pymysql.escape_string(str(articleDetail))
cursor.execute("insert into EnglishNovels_chapterdetail (`chapterindex`, `content`, `belongbook_id`) values(%d, '%s', %d)" % (chapterNum, articleDetail, 5))

成功解决~

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页