Justin Davis wrote:
> Anyone know how to make MySQL return Unicode for text fields?  My
> schema looks sort of like this:
>
> TABLE users (
>     int id NOT NULL auto_increment,
>     first TEXT NOT NULL,
>     primary key (id)
> ) CHARSET=utf8
>
> Yet if I do a query via webpy....
>
> user = mydb.select("users")[0]
> type(user.first)
>     --- "str"
>
> Am I doing something wrong?  Is this a problem
> with the MySQL bindings for python?

I'd say there is no problem. Unicode is a collection of codes
identifying many characters in many different language scripts (all,
presumably). Python unicode() objects are just a way of representing
Unicode text, but they are not valid byte sequences. What MySQL gives
you is a UTF-8 encoded string, which is a way of encoding Unicode into
valid byte sequences. You may convert it to a unicode() object as
follows:

user = unicode(user.first)

Higher-level ORMs such as SQLObject and SQLAlchemy might take care of
this implicitly, but do take note that you will have re-encode your
data again in UTF-8 prior to writing it to the client.

--Jonas Galvez
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web.py" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/webpy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to