pymysql 无法识别 PuTTY 上的®(注册商标符号)

pymysql is not recognizing ® (the registered trademark symbol) on PuTTY

如果我尝试 运行 一个包含这一行的 python 文件:

cursor.execute("SELECT GameName FROM GamesTable WHERE GameName = 'Sid Meier's Civilization® IV'")

PuTTY 给我错误:

pymysql.err.ProgrammingError: (1064, u"You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right syntax to use near 's
Civilization\xae IV'' at line 1")

所以它显然无法读取® 符号。

我搜索了一下,有人说可以确定远程字符集是 UTF-8,确实是。

此外,PuTTY 似乎可以很好地处理符号 - 如果我只写 print "®" 然后它会正确打印符号。只有当尝试使用 pymysql 到达 sql 服务器时,它似乎有问题。

unicode 错误? (假设 python 2)

gname = "Sid Meier's Civilization" + unichr(174) + "IV'"

然后

gname.encode('utf-8')

这给出了 "Sid Meier's Civilization\xc2\xae IV"

https://docs.python.org/2/howto/unicode.html