python 中和php addslashes的方法
2019-08-22 python 2348
数据库转义,有两种python自带的
pymysql.escape_string
MySQLdb.escape_string
经过多番测试自定义了python方法:
def addslashes(s): if(isinstance(s,str)): d = {"\0": ""," ": " ","\\":"\\\\", "'": "\\'", "\"": "\\\"","\r":"\\r","\n":"\\n"} for x in d: s = s.replace(x, d.get(x)) return "'"+s+"'" # return "'"+pymysql.escape_string(s)+"'" elif s is None: return "NULL" else: return "'"+str(s)+"'"
很赞哦! (0)
相关文章
文章评论
-
-
-
0条评论