1、错误1054
sql="SELECTword,weight,topic FROM wordfrommovie WHERE tag=%s "%(name)
cymysql.err.InternalError: (1054, "Unknown column '爱情' in 'where clause'")
单从字面理解,我们很容易得出列名不存在的结论,但是,很多时候起始并不是由于列名出错造成的。而是由于拼凑sql语句时对字符类型数据没有用引号引起来造成的。根据sql语句,我们希望得到的时tag = 爱情,报错的原因是tag = '爱情',多了单引号,因此出现了列名错误
sql中如果%S传入的是整型的倒不会出现什么错误,而如果sql中字符串类型必须要包含在引号内。
int =%s
str ='%s'
改成sql="SELECTword,weight,topicFROMwordfrommovieWHEREtag='%s'"%(name)
则错误消失
2、错误1064
1064通常是语法错误,首先检查语法,若语法正确,要考虑传入的%S中是否包含'或“
(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 ''爱情' set movie_topic =null' at line 1")可以看到爱情后面多了一个单引号
如果包含可以在字符串传入之前进行转义,name为要传入的字符串
name .replace("'","\\\'") //将字符串中包含单引号的地方用斜杠代替