Skip to content

Instantly share code, notes, and snippets.

@irskep
Created January 10, 2014 01:31
Show Gist options
  • Save irskep/8345460 to your computer and use it in GitHub Desktop.
Save irskep/8345460 to your computer and use it in GitHub Desktop.
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm.session import Session
from sqlalchemy import (
create_engine,
Column,
Integer,
UnicodeText,
)
Base = declarative_base()
class DemoTable(Base):
__tablename__ = 'demo_table'
id = Column(Integer(), primary_key=True)
unicode_text = Column(UnicodeText())
# there's probably a simpler way to configure all this
engine = create_engine(
'mysql://root:@blah/the_database?charset=utf8',
encoding='utf8')
Base.metadata.create_all(bind=engine)
session = Session(bind=engine)
### THE MEAT ###
session.add(DemoTable(unicode_text=u"What a lovely day"))
session.flush()
session.expire_all()
print type(session.query(DemoTable).first().unicode_text)
# <type 'str'>
engine.dispose()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment