3

My question: Why can't I retrieve non-ascii chars from sqlalchemy db into python?

In more detail: I have an entry with non-ascii char in my sqlalchemy db (Umlaut for example). When I try to retrieve this entry by using:

q=self.session.query(Table_Name).filter(Table_Name.id=='some id')
q.all()

I receive the following error:

UnicodeEncodeError: 'ascii' codec can't encode characters in position

I solved it by decoding the entries into UTF-8 before inserting the entry to the DB.

Yet I wondered - why does this error appeared? Is there an option to fetch entries already encoded as unicode for example instead of bytechar\ascii into python?

1
  • Is your db UTF-8 and you have # -- coding: utf-8 -- in python too? Commented Jun 9, 2016 at 13:52

1 Answer 1

1

Per the docs, you should specify the encoding in the connection string:

e = create_engine("mysql+pymysql://scott:tiger@localhost/test?charset=utf8")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.