[egenix-users] Unicode problem

Moof moof at metamoof.net
Thu Dec 29 12:05:07 CET 2005


On 12/29/05, Andrew Veitch <andrew at logicalware.com> wrote:
>
> I'm in the process of adding Microsoft SQL Server support to
> MailManager. Currently it supports PostgreSQL (psycopg) and MySQL.
> I've bought a copy of mxODBC Zope DA.
>
> Unfortunately I keep getting a "TypeError: command must be a string".
> Doing some quick Googling suggested this was a Unicode problem and
> when I changed the input to ordinary strings it works fine.
>
> Obviously we could get MailManager to check if it is using the mxODBC
> and then avoid Unicode if it is but that would be quite a lot of work
> and something I'm keen to avoid if there is a work around.


It's a bit of an ambiguous error message.

The command must be a string, this seems to be a limitation of mxODBC.
However, the values in a prepared statement can be unicode if necessary, and
mXODBC handles that situation correctly.

This is wrong and raises the above error:

cursor.execute(u"INSERT INTO table1 (value1) VALUES 'áéíóú'")

This works:

cursor.execute('INSERT INTO table1 (value1) VALUES ?', (u'áéíóú',))

...and also gives you the advantage of doing all your escaping for you, all
the better to avoid SQL injection attacks, as well as caching execution
paths for complex queries.

Yes, it's annoying, as most ORM packages these days seem to generate unicode
strings, which makes mxODBC of limited use with them. I'm not sure if this
is a limitation of ODBC itself, but if it isn't, is there any chance of
getting mxODBC working with these strings?

Moof
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /mailman-archives/egenix-users/attachments/20051229/716010b5/attachment-0138.htm


More information about the egenix-users mailing list