A
A
Alexey Andrianov2014-03-31 21:41:51
Python
Alexey Andrianov, 2014-03-31 21:41:51

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

I get in response:
Traceback (most recent call last):
File "C:/Users/�������/PycharmProjects/untitled/download_files.py", line 109, in
"%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s")', row)
File "H:\Developer\python2\lib\ site-packages\MySQLdb\cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "H:\Developer\python2\lib\site-packages\MySQLdb\connections.py", line 36 , in defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.OperationalError: (1366, "Incorrect string value: '\\xD0\\x9C\\xD0\\xB0\\xD1\\x88...' for column 'a_name' at row 1 ")
Actually the question is - how to win it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene, 2014-03-31
@Nc_Soft

Set the table to the correct encoding, write in the correct encoding. *Correct encoding is utf-8

V
Valentine, 2014-04-01
@vvpoloskin

Most likely there is a jamb in the editor in which you write. Force utf-8 encoding in it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question