I use sqlalchemy to create my database schema. I have not succeeded in using utf-8, no matter what I tried.
Below is a minimal python script that recreates my problem:
from sqlalchemy import create_engine, Column, Unicode from sqlalchemy.ext.declarative import declarative_base engine = create_engine('mysql+mysqldb://user:password@localhost/multidic?charset=utf8', echo=True) Base = declarative_base() class MyTableName(Base): __tablename__ = "mytablename" test_column = Column(Unicode(2),primary_key=True) Base.metadata.create_all(engine)
After running this script, when I look at the database, I see that the encoding is latin1 instead of utf-8:
mysql> SHOW FULL COLUMNS FROM mytablename; +-------------+------------+-------------------+------+-----+---------+-------+---------------------------------+---------+ | Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment | +-------------+------------+-------------------+------+-----+---------+-------+---------------------------------+---------+ | test_column | varchar(2) | latin1_swedish_ci | NO | PRI | NULL | | select,insert,update,references | | +-------------+------------+-------------------+------+-----+---------+-------+---------------------------------+---------+ 1 row in set (0.00 sec)
I tried changing the type of the created column ( String instead of Unicode ) and also tried to add the encoding = "utf8" argument to the create_engine call, but none of this worked .
So my question is:
How to ensure the use of a given character encoding (utf-8 in my case) in MySQL with sqlalchemy?
Thanks:)
Notes:
I am using sqlalchemy 0.7 and python 2.7; I can update one or both, but only if this is the only solution!
I have mysql 5 and it supports utf-8:
mysql> show character set where charset="utf8"; +---------+---------------+-------------------+--------+ | Charset | Description | Default collation | Maxlen | +---------+---------------+-------------------+--------+ | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | +---------+---------------+-------------------+--------+ 1 row in set (0.00 sec)
python mysql encoding utf-8 sqlalchemy
Jealie
source share