Python MySQL escape special characters

前端 未结 4 1764
感动是毒
感动是毒 2020-12-16 05:34

I am using python to insert a string into MySQL with special characters.

The string to insert looks like so:

macaddress_eth0;00:1E:68:C6:09:A0;macadd         


        
4条回答
  •  没有蜡笔的小新
    2020-12-16 06:06

    Although I also think parameter binding should be used, there is also this:

    >>> import MySQLdb
    >>> example = r"""I don't like "special" chars ¯\_(ツ)_/¯"""
    >>> example
    'I don\'t like "special" chars \xc2\xaf\\_(\xe3\x83\x84)_/\xc2\xaf'
    >>> MySQLdb.escape_string(example)
    'I don\\\'t like \\"special\\" chars \xc2\xaf\\\\_(\xe3\x83\x84)_/\xc2\xaf'
    

提交回复
热议问题