I want read some record from one mysql DB and write to another DB. I am doing like this:
cnx_1 = mysql.connector.connect(config_1)
cnx_2 = mysql.connector.connect(config_2)
cursor_1 = cnx_1.cursor()
cursor_2 = cnx_2.cursor()
query = "select * from...."
cursor_1.execute(query)
for res in cursor_1:
id,msg=res
insert_query="insert into tbl1 (id,msg) values (%d,'%s')"%(id,msg)
cursor_2.execute(insert_query)
This code works fine in most cases,
Problem: but if the 'msg' field contains special character, i have problem.
first i replaced some special char with equivalent one:
msg=msg.replace('\'',"\\'")
msg=msg.replace('\r',"\\r")
msg=msg.replace('\n',"\\n")
and then i found a better solution from here:
msg=str(MySQLdb.escape_string(msg))
but both was not fully successful because msg can be whatever. for example for 2nd solution i got this error:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xc9' in position 683: ordinal not in range(128)