Hello,
i want to lern SQLAlchemy. But i have problems with encodings at a
query.
Here is a example (copy of the tutorial 0.5)...
This is the example (is ready to use).
#-------------------------------------------------------------------------------------
#!/usr/bin/python
#-*- coding: utf-8 -*-
import sqlalchemy
print "SQL Alchemy Version", sqlalchemy.__version__ # 0.5rc2
from sqlalchemy import *
from sqlalchemy.orm import *
#from sqlalchemy.ext.declarative import declarative_base
#from sqlalchemy import ForeignKey
#from sqlalchemy.orm import relation, backref
engine = create_engine('sqlite:///tutorial_sqlalchemy05.sqlite', echo
= True,
encoding="utf-8")
Session = sessionmaker(bind=engine)
session = Session()
#------------------------------------------------------------------------------
metadata = MetaData()
#Base = declarative_base()
#------------------------------------------------------------------------------
users_table = Table('users', metadata,
Column('id', Integer, primary_key=True),
Column('name', String),
Column('fullname', String),
Column('password', String))
#------------------------------------------------------------------------------
class User(object):
def __init__(self, name, fullname, password):
self.name = name
self.fullname = fullname
self.password = password
def __repr__(self):
return "<User('%s','%s', '%s')>" % (
self.name, self.fullname,
self.password)
#------------------------------------------------------------------------------
mapper(User, users_table)
metadata.create_all(engine)
#------------------------------------------------------------------------------
ed_user = User('ed', 'Ed Jones', 'edspassword')
umlaut_user = User('Mit', 'Umlaut', 'EinÖ')
session.add(ed_user)
session.add(umlaut_user)
session.commit()
query = session.query(User).filter(User.name == "Mit")
print query.all()
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
This is the Error Message:
[...]
2008-10-26 08:01:59,624 INFO sqlalchemy.engine.base.Engine.0x...0ecL
['Mit']
[Traceback (most recent call last):
File "./EncodingFehler", line 46, in <module>
print query.all()
UnicodeEncodeError: 'ascii' codec can't encode character u'\xd6' in
position 26: ordinal not in range(128)
Insert "öüäßÄÖÜ" is no problem. In sqlite-gui it is correct.
But no Query where one result has a German umlaut.
I found this Problem in the Google Group.
But no solution... it is a SQL Alchemy issue.
Any workaround? If i delete
def __repr__(self):
return "<User('%s','%s', '%s')>" % (
self.name, self.fullname,
self.password)
i get back the refrences. But i can't use it...
I use SQLA 0.5rc2, Python 2.5 and Ubuntu 8.04 LTS.
Thanks.