unicode in sqlobject / mysql

32 views
Skip to first unread message

Andreas Reuleaux

unread,
Dec 9, 2005, 2:14:14 PM12/9/05
to turbo...@googlegroups.com
Instead of defining individual columns
to hold unicode data in sqlobject

class Mytable(SQLObject)
mycol=UnicodeCol()
...

I would rather use normal string columns

class Mytable(SQLObject)
mycol=StringCol()
...

and declare the whole database to use utf-8.
I can do this in mysql

alter database mydb character set utf8

and then I can fill in unicode strings from python mysqldb

>>> ustr=someunicodestring
>>> import MySQLdb
>>> con=MySQLdb.connect(...)
>>> c=con.cursor()
>>> c.execute("insert into mytable (mycol) values (%s)", ustr)

but I can't from sqlobject:

# mycol=StringCol() as above

from model import Mytable
m=Mytable()
mytable.mycol=ustr


File "/opt/tg/lib/python2.4/site-packages/SQLObject-0.7.0-py2.4.egg/sqlobject/main.py", line 1031, in _SO_setValue
dbValue = from_python(value, self._SO_validatorState)
File "/opt/tg/lib/python2.4/site-packages/SQLObject-0.7.0-py2.4.egg/sqlobject/col.py", line 498, in from_python
return value.encode("ascii")
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 1: ordinal not in range(128)


Any advice?

-Andreas

Gábor Farkas

unread,
Dec 9, 2005, 7:35:52 PM12/9/05
to turbo...@googlegroups.com

try "mytable.mycol=ustr.encode('utf8')".

if i understand your problem correctly, you told sqlobject that that
field contains non-unicode string. so when you pass sqlobject an unicode
string, he tries to convert it to a non-unicode string.

and it does it using the "default" charset for python. which is defined
as "ascii". and of course for most of the unicode characters, there's no
way to represent them in 'ascii'. that's why it fails.

so it's better to encode to utf8 before using sqlobject.

(assuming that really this was the problem ;)

gabor

Andreas Reuleaux

unread,
Dec 10, 2005, 2:31:53 AM12/10/05
to turbo...@googlegroups.com

Yes, that did it. - I should have known.
Thanks a lot.

-Andreas

> if i understand your problem correctly, you told sqlobject that that
> field contains non-unicode string. so when you pass sqlobject an unicode
> string, he tries to convert it to a non-unicode string.
>
> and it does it using the "default" charset for python. which is defined
> as "ascii". and of course for most of the unicode characters, there's no
> way to represent them in 'ascii'. that's why it fails.
>
> so it's better to encode to utf8 before using sqlobject.
>
> (assuming that really this was the problem ;)
>
> gabor
>
>

> !DSPAM:439a241b56665336129849!

Reply all
Reply to author
Forward
0 new messages