开发者

Storing windows path in MySQL without escaping backslashes

I would like to store windows path in MySQL without escaping the backslashes. How can I do this in Python? I am using MySQLdb to insert records into the database. When I use MySQLdb.escape_string(), I notice that the开发者_如何学编程 backslashes are removed.


Have a look at os.path.normpath(thePath)

I can't remember if it's that one, but there IS a standard os.path formating function that gives double backslashes, that can be stored in a db "as is" and reused later "as is". I have no more windows machine and cannot test it anymore.


Just use a dictionary to add slashes wherever required to make the query valid :

http://codepad.org/7mjbwKBf

def addslashes(s):
    dict = {"\0":"\\\0", "\\":"\\\\"}  #add more here
    return ''.join(dict.get(x,x) for x in s)

query = "INSERT INTO MY_TABLE id,path values(23,'c:\windows\system\')";

print(addslashes(query));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜