easiest way here would be to specify use_unicode=1&charset=utf8 with the MySQL 
connection string, so that your MySQLdb DBAPI handles incoming unicode objects 
correctly.

http://docs.sqlalchemy.org/en/rel_0_7/dialects/mysql.html#unicode




On Jul 18, 2012, at 7:45 AM, Cornelius Kölbel wrote:

> Hi there,
> 
> I am trying to do a select on a table, where a user has a /username/ with a 
> German umlaut like "kölbel".
> The table in the mysql-database is utf-8. I also failed when the table was 
> latin1.
> 
> My problem is that by no(!) chance I manage to match the user and get a row 
> from the select statement. (see version2)
> 
> Should it be possible at all or am I just screwing up with my non-existent 
> unicode-skills! ;-)
> 
> Thanks a lot and kind regards
> Cornelius
> 
> # -*- coding: utf-8 -*-
> from sqlalchemy import create_engine
> from sqlalchemy import MetaData
> from sqlalchemy.orm import sessionmaker
> from sqlalchemy import Table
> 
> CONNECT_STRING="mysql://my_connect_string"
> TABLE = "linotp_user"
> USER = u"kölbel"
> 
> engine  = create_engine(CONNECT_STRING, echo=False)
> meta    = MetaData()
> Session = sessionmaker(bind=engine)
> session = Session()
> table   = Table(TABLE, meta, autoload=True, autoload_with=engine)
> 
> print "type of user: ", type(USER)
> select = table.select(u"username = '%s'" % USER)
> 
> print "select statement: ", select
> print "type of select: ", type(select)
> 
> print "Printing rows, version 1"
> rows = session.execute(select)
> for row in rows:
>     print " ::: ", row
>     
> print "Printing rows, version 2"
> sel_string=u"select * from %s where username = '%s'" % (TABLE, USER)
> print type(sel_string)
> print sel_string
> rows = engine.execute(sel_string)
> for row in rows:
>     print " ::: ", row
> 
> session.close()
> 

Attachment: PGP.sig
Description: This is a digitally signed message part

Reply via email to