当前位置: 代码迷 >> 综合 >> (python3异常)1064, You have an error in your SQL syntax; check the manual...at line 1
  详细解决方案

(python3异常)1064, You have an error in your SQL syntax; check the manual...at line 1

热度:101   发布时间:2023-12-02 07:26:29.0

python中用pymysql模块操作mysql出现以下异常:

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 ****)’ at line 1”)

sql串为:

sql = """insert into student(name,age,class) values('%s','%s','%s')""" 
% (name,age,cla)

原因可能是占位符对应的字符串中含有mysql敏感的字符引起的,在做爬虫时,爬下的数据中各种格式都有,可能会出现这个异常。

用pymysql模块中自带的规范字符的方法处理一下即可:

sql = """insert into student(name,age,class) values('%s','%s','%s')""" 
% (escape_string(name),escape_string(age),escape_string(cla))
  相关解决方案