【发布时间】:2016-04-09 05:11:11
【问题描述】:
我在 python 2.7 中使用 mysql.connector python 库
我有一个 unicode 字符串,它可能包含也可能不包含单引号和双引号。 以下是我为逃生功能尝试的事情:
def escape(string):
#string.MySQL.escape_string()
#string = string.decode('string_escape')
#string = string.encode('unicode-escape').replace("'", "''")
#string = string.encode('unicode-escape').replace('"', '\"')
#string = string.encode('unicode-escape').replace("'", u"\u2019")
#string = string.encode('unicode-escape').replace('''"''', u"\u201D")
#string = string.encode('unicode-escape').replace('''''', u"\u201D")
return string
似乎没有任何效果。我尝试使用 this 函数但仍然给出 mysql 语法错误。
我需要 mysql.connector 库中的一些东西,它可以在不破坏 unicode 和 mysql 查询的情况下转义单引号和双引号。
这是我正在使用的 SQL 查询示例:
"""SELECT * FROM messages WHERE msg_id = '{msg_id}'""".format(**db_dict)
如果需要更多详细信息,请告诉我
编辑:示例 SQL 查询已更新
【问题讨论】: