sql注入中最多見的就是字符串拼接,研發人員對字符串拼接應該引發重視,不該忽略。python
錯誤用法1:mysql
sql = "select id, name from test where id=%d and name='%s'" %(id, name) sql
cursor.execute(sql)微信
錯誤用法2:函數
sql = "select id, name from test where id="+ str(id) +" and name='"+ name +"'" 字符串
cursor.execute(sql)string
正確用法1:test
args = (id, name) select
sql = "select id, name from test where id=%s and name=%s" sql語句
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,能夠對字符串轉義。
更多內容,請掃碼關注微信公衆號「程序媛蒲葦」