Maoritzio
Maoritzio

Reputation: 1242

SQLAlchemy and python - retrieving non-ascii chars from DB

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?

Upvotes: 3

Views: 1120

Answers (1)

Sam
Sam

Reputation: 4090

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

e = create_engine("mysql+pymysql://scott:tiger@localhost/test?charset=utf8")

Upvotes: 1

Related Questions