0

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?

Mahesh Gurav
  • 153
  • 3
  • 10

1 Answers1

2
  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.

OShadmon
  • 704
  • 5
  • 11
  • This did not help at all for my `utf8mb4` database when I used `use_unicode=True, charset='utf8'`. What did help was [this anser](https://stackoverflow.com/a/43836817/2550406) – lucidbrot Aug 24 '19 at 14:45