Encoding issue in python using mysql connector python

5.8k views Asked by At

I have written a python script which get data from database and process it. Initially I was using pymysql for connecting database which I changed to MySQL Connector for python. When I started using MySQL Connector I am getting following issue

'ascii' codec can't encode characters in position 14-16: ordinal not in range(128)

I tried by adding charset settings of MySQL Connector but issue still persists.

Do anyone have idea on this issue?

1

There are 1 answers

1
OShadmon On BEST ANSWER
  1. make sure that in your table you have specified a ascii encoding

Example:

CREATE TABLE t1(
     col1 char, 
     ....,
     ....,
     ....
)Engine=InnoDB charset=ascii;
  1. In the MySQL-Python connector specify assci encoding and enable unicode

db = MySQLdb.connect( host="localhost", port=3306, user="john", passwd="megajonhy", db="jonhydb", use_unicode=True, charset='ascii' )

I believe that by default use_unicode is set to False, and thus by setting it True you will fix the issue.