Answer the question
In order to leave comments, you need to log in
How to fix Unicode issues when writing to MySQL DB?
Good evening.
I fight for an hour over the problem:
...
DB = MySQLdb.connect(host="localhost", user="root", passwd="123", db="123", charset='utf8')
...
Cursor = DB.cursor()
delimiter = ';'
csv_data = codecs.open('tmp/' + item, 'rb', encoding='utf-8')
next(csv_data)
for line in csv_data:
print line
row = line.split(delimiter)
print row
Cursor.execute('INSERT INTO cdr(device_id, connection_date_time, call_duration, call_disconnection_cause, \
call_information, a_ip, a_type, a_name, a_number, a_mod_number, b_ip, b_type, b_name, b_number, b_mod_number, \
incoming_date_time, disconnection_date_time, SMS) VALUES("%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", \
"%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s")', row)
DB.commit()
Answer the question
In order to leave comments, you need to log in
Set the table to the correct encoding, write in the correct encoding. *Correct encoding is utf-8
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question