Working with utf8 (like Chinese) with sqlAlchemy + Elixir

Today I’ve been playing around with sqlAlchemy and Elixir.

I was having problems with how some strings (Chinese Characters) were being encoded and stored. The issue was with the

metadata.bind statement (create_engine() without Elixir)

before it was:

metadata.bind = 'mysql://username:password@localhost/database'

You need to select utf8 encoding manually.

metadata.bind = 'mysql://username:password@localhost/database?charset=utf8'

or, plain SQLAlchemy:

db = create_engine('mysql://...', encoding='utf8')

  • Eduardo Nunes Pereira

    That solves my problem, thanks !