Search code examples
pythonsqlputtypymysql

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


If I try to run a python file with this line in it:

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

PuTTY gives me the error:

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")

So it clearly can't read the ® symbol.

I've searched around and people said to be sure that the Remote Character Set is UTF-8, which it is.

Also, PuTTY seems to handle the symbol fine - if I just write print "®" then it correctly prints the symbol. It is only when trying to use pymysql to reach the sql server that it seems to have a problem.


Solution

  • unicode error ? (assuming python 2)

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

    then

    gname.encode('utf-8')

    which gives "Sid Meier's Civilization\xc2\xae IV"

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