码迷,mamicode.com
首页 > 数据库 > 详细

Python防止sql注入

时间:2017-06-23 13:36:44      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:sql   username   ...   pos   pytho   data   网上   nal   不同   

看了网上文章,说的都挺好的,给cursor.execute传递格式串和参数,就能防止注入,但是我写了代码,却死活跑不通,怀疑自己用了一个假的python

 

最后,发现原因可能是不同的数据库,对于字符串的占位定义不同,这段话:

Note that the placeholder syntax depends on the database you are using

‘qmark‘ Question mark style, e.g. ‘...WHERE name=?‘ 
‘numeric‘ Numeric, positional style, e.g. ‘...WHERE name=:1‘ 
‘named‘ Named style, e.g. ‘...WHERE name=:name‘ 
‘format‘ ANSI C printf format codes, e.g. ‘...WHERE name=%s‘ 
‘pyformat‘ Python extended format codes, e.g. ‘...WHERE name=%(name)s‘

 

我理解,就是有多种占位方式,而我一棵树上吊死,光试验%s了,所以每次都报这个错:

rs=c.execute("select * from log where f_UserName=%s","jetz")

OperationalError: near "%": syntax error

 

换一个试试,

rs=c.execute("select * from log where f_UserName=:usr",{"usr":"jetz"})

可以

再试:

rs=c.execute("select * from log where f_UserName=:1 ",["jetz"])

也可以

看了sqlite对%比较过敏

Python防止sql注入

标签:sql   username   ...   pos   pytho   data   网上   nal   不同   

原文地址:http://www.cnblogs.com/jetz/p/7069425.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!