Andreas Reuleaux wrote:
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?


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

Reply via email to