Python中如何防止sql注入
2017-10-28 22:19
274 查看
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,可以对字符串转义。
更多内容,请扫码关注微信公众号“程序媛蒲苇”
错误用法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注入?
- java持久层框架mybatis如何防止sql注入
- mybatis如何防止sql注入
- EntityFramework Core 2.0执行原始查询如何防止SQL注入?
- MyBatis如何防止SQL注入
- 如何防止sql注入
- mybatis是如何防止SQL注入的
- mybatis如何防止sql注入
- 如何防止sql注入
- MyBatis如何防止SQL注入
- java如何防止sql注入
- PHP中如何防止SQL注入
- 什么是sql注入,如何来防止sql注入
- SQL注入漏洞产生的原因 ? 如何防止?
- mybatis如何防止sql注入(1)
- php网站如何防止sql注入?(PHP注入的安全规范)
- 如何防止web项目中的sql注入
- mybatis如何防止sql注入
- 什么叫做SQL注入,如何防止
- MyBatis如何防止SQL注入