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))