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

Python中如何防止sql注入

时间:2017-10-28 18:58:57      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:语句   nbsp   分享   防止   数位   扫码   test   .exe   字符串   

  sql注入中最常见的就是字符串拼接,研发人员对字符串拼接应该引起重视,不应忽略。

  错误用法1:

    sql = "select id, name from test where id=%d and name=‘%s‘" %(id, name)

    cursor.execute(sql)

  错误用法2:

    sql = "select id, name from test where id="+ str(id) +" and name=‘"+ name +"‘"

    cursor.execute(sql)

  正确用法1:

    args = (id, name)

    sql = "select id, name from test where id=%s and name=%s"

    cursor.execute(sql, args)

    execute()函数本身有接受sql语句参数位的,可以通过python自身的函数处理sql注入问题。

  正确用法2:

    name = MySQLdb.escape_string(name)

    sql = "select id, name from test where id=%d and name=‘%s‘" %(id, name)

    cursor.execute(sql)

    python模块MySQLdb自带针对mysql的字符转义函数escape_string,可以对字符串转义。

 

  更多内容,请扫码关注微信公众号“程序媛蒲苇”

    技术分享

 

Python中如何防止sql注入

标签:语句   nbsp   分享   防止   数位   扫码   test   .exe   字符串   

原文地址:http://www.cnblogs.com/puwei222/p/7747545.html

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