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?

1
  • Paste code sample please? Commented Dec 24, 2016 at 9:37

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This did not help at all for my utf8mb4 database when I used use_unicode=True, charset='utf8'. What did help was this anser

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.