I use Twisted to asynchronously access our database in Python. My code is as follows:
from twisted.enterprise import adbapi from MySQLdb import _mysql as mysql ... txn.execute(""" INSERT INTO users_accounts_data_snapshots (accountid, programid, fieldid, value, timestamp, jobid) VALUES ('%s', '%s', '%s', '%s', '%s', '%s') """ % (accountid, programid, record, mysql.escape_string(newrecordslist[record]), ended, jobid))
This worked until I came across this character: ®, which caused the thread to throw an exception: `exceptions.UnicodeEncodeError: 'ascii' codec can't encode character u'\xae' in position 7: ordinal not in range(128)
However, if I do not use MySQLdb_mysql.escape_string (), I get database errors when the input contains quotes, etc. (sure). An exception occurs before accessing the database, so database mapping does not matter at all.
What is the best way to avoid this content without exception for Unicode characters? The ideal solution is one where I can pass Unicode characters that will not interfere with a MySQL query without problems; however, deleting the Unicode character string, replacing them with question marks, distorting them or anything else that stops the outputs would be acceptable.
source share