[DB-SIG] Passing Unicode strings to Database

Arthur Albano arthuralbano@hotmail.com
Sun, 10 Mar 2002 00:24:00 +0900

>From: Mikhail Astafiev <mikle@tomsk.net>
>Reply-To: Mikhail Astafiev <mikle@tomsk.net>
>To: db-sig@python.org, python-list@python.org
>Subject: [DB-SIG] Passing Unicode strings to Database
>Date: Wed, 6 Mar 2002 18:11:17 +0700
>[Env: Win2k, Python 1.5.2]
>Hi people!
>I have a string in Japanese encoded in UTF8. I need to put it into
>database in Unicode form.
>import dbi, odbc
>runes = 
>operator="""INSERT INTO %s VALUES('%s','%s');"""%(self.ODBC_table,
>    int(self.pr_table[row]["pr_no"]),
>    runes.tostring())
>c.execute(operator)    # update database
>I'm getting the following error trace:
>Traceback (innermost last):
>   File "C:\dev\Products\MRATES~1\server.py", line 34, in ?
>     PR.sync_all()
>   File "C:\dev\Products\MRATES~1\PRML\code\prml.py", line 2837, in 
>     self.__sync_with_sql__(i)
>   File "C:\dev\Products\MRATES~1\PRML\code\prml.py", line 2797, in 
>     c.execute(operator)    # update database
>TypeError: argument 1: expected string without null bytes, string found
>runes is array.array('H') containing Unicode string.
>Database string field has "ntext" type. How can I overcome this
>Thanks in advance,


If the problem is just conversion from UTF-8 to UNICODE, try this:


It returns a unicode string.

Arthur Albano
"Just a Weekend Programmer"

Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp.