先看这个查询语句:SELECT * FROM company WHERE CompanynameChinese LIKE '%条件%'
这语句在mysqldb的模块中会导致被转义。因为%在python中是转义字符,需要两个%%代替%,但是我尝试这样写:
SELECT * FROM company WHERE CompanynameChinese LIKE '%%%%%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 '条件'%%'' at line 1")
下面是解决的办法:
在python的mysqldb中,他已经会做一些字符的转义. 比如你传进一个字符串,你不需用“ ”引起来,因为mysqldb会判断你的字符是否是整形或者是字符串,然后顺手帮你转义。
正确sql语法是:
sql = """SELECT * FROM company WHERE CompanynameChinese LIKE %s"""
params=[key]
mysql.get_all(sql,params)
最后,
尽情的传参吧!!!