凌的博客

您现在的位置是: 首页 > 学无止境 > python > 

python

python 中和php addslashes的方法

2019-08-22 python 1675

数据库转义,有两种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条评论