Passing Unicode strings to Database 
Author Message
 Passing Unicode strings to Database

Quote:

> [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
> o=odbc.odbc(self.ODBC_name+"/"+self.ODBC_sql_username+"/"+self.ODBC_sql_password)
> c=o.cursor()
> runes = UTF82Runes(string.replace(self.pr_table[row]["description"],"'","''"))
> 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 sync_all
>     self.__sync_with_sql__(i)
>   File "C:\dev\Products\MRATES~1\PRML\code\prml.py", line 2797, in __sync_with_sql__
>     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
> problem?

Use mxODBC ? It has Unicode support builtin. Whether your ODBC driver
has is a different question, though.

--
Marc-Andre Lemburg
CEO eGenix.com Software GmbH
______________________________________________________________________
Company & Consulting:                           http://www.*-*-*.com/
Python Software:                   http://www.*-*-*.com/



Sun, 22 Aug 2004 19:20:05 GMT  
 Passing Unicode strings to Database

Quote:



>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
>o=odbc.odbc(self.ODBC_name+"/"+self.ODBC_sql_username+"/"+self.ODBC_sql_password)
>c=o.cursor()
>runes =
>UTF82Runes(string.replace(self.pr_table[row]["description"],"'","''"))
>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
>sync_all
>     self.__sync_with_sql__(i)
>   File "C:\dev\Products\MRATES~1\PRML\code\prml.py", line 2797, in
>__sync_with_sql__
>     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
>problem?

>Thanks in advance,
>Mick.

Mikhail,

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

unicode(string_in_utf8_to_be_converted,'UTF-8')

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.



Wed, 25 Aug 2004 23:24:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Passing Unicode strings to Database

2. ANNOUNCE: unicode 0.4, command line unicode database query tool

3. Tkinter wart: returned texts are sometimes strings, sometime Unicode strings

4. unicode strings and strings mix

5. How does one pass Unicode command line arguments to Tcl program (Windows 2000)

6. Unicode strings

7. Unicode to string conversion HELP

8. Unicode strings

9. Unicode strings

10. Unicode strings

11. Unicode string for the standard library ?

12. unicode to 'normal' string conversion

 

 
Powered by phpBB® Forum Software