这是一个创建于 4225 天前的主题,其中的信息可能已经有所发展或是发生改变。
大概是这样的
common = form["common"]
query = "UPDATE tb SET %s = %s WHERE id = %s"
cursor.execute(query, [common, field, updtae_id])
变量common代替字段名title。
然后common替代第一个 %s 时,SQL报错:
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 ''title' = 'title3' WHERE id = '3'' at line 1"
就是 common 被自动加上引号了……如何才可以不让它有引号呢?
Python新手,谢谢指教……
3 条回复 • 1970-01-01 08:00:00 +08:00
 |
|
1
nybux 2013-08-12 11:46:27 +08:00 1
你这种不是变量绑定,是动态sql了,你还是自己先在字符串层面把column都补好
|
 |
|
2
janxin 2013-08-12 14:49:23 +08:00 1
楼主用的什么SQL库呢?如果是Mysql-python的话,可以用: ``` MySQLdb.escape_string() ```
还有楼主你这么写是有SQL注入漏洞的....
|
 |
|
3
lqs 2013-08-12 16:44:47 +08:00 1
@ janxin 楼主这样是参数化查询,会自动判断类型并转义,不会有SQL注入漏洞。反而手动去escape_string容易漏写。
|