can't get a query with german umlaute (äöüß) (utf8 Problem)

541 views
Skip to first unread message

dunkelgruen

unread,
Oct 26, 2008, 2:23:25 PM10/26/08
to sqlalchemy
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.

Michael Bayer

unread,
Oct 26, 2008, 3:32:41 PM10/26/08
to sqlal...@googlegroups.com
use the Unicode type instead of the String type, or alternatively set
convert_unicode=True on your create_engine() call.

Sebastian Preuß

unread,
Oct 26, 2008, 5:43:41 PM10/26/08
to sqlal...@googlegroups.com
Sorry that dosn't work.
I try:

engine = create_engine('sqlite:///tutorial_sqlalchemy05.sqlite',

convert_unicode=True) No.

u"xxx" No.

Combination... No

Michael Bayer

unread,
Oct 26, 2008, 5:56:13 PM10/26/08
to sqlal...@googlegroups.com
oh, sorry, this is not the issue in this specific case as you're using
SQLite which represents everything as unicode anyway, although my
solution is required if you're going to use a database like
postgres. The specific traceback here is because __repr__ is not
allowed to return a non-ascii string in python. Change it to
__str__(), or alternatively change the "%s" tokens to "%r" to produce
a repr() of each element.
Reply all
Reply to author
Forward
0 new messages