zoukankan      html  css  js  c++  java
  • Python中如何防止sql注入

      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,可以对字符串转义。

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

        

  • 相关阅读:
    安装devstack之配置proxy
    设备信息表项目
    好的运维工程师
    rhel 6.4 增加光盘为yum repo
    深度运维产品工具关键词
    坚持是一种能力
    书单 电影单 电视剧单
    三日不读书,便觉得言语无味,面目可憎
    STAR法则
    【断舍离】
  • 原文地址:https://www.cnblogs.com/puwei222/p/7747545.html
Copyright © 2011-2022 走看看