MySQL Forums
Forum List  »  Connector/Python

insert unicode string
Posted by: Ivan Horvath
Date: August 25, 2006 06:49AM

Dear All,

i have some problem when i try to insert in a table a unicode string.
i've made many trials without any success.

what is really strange for me, that the following code in #144-145 gives latin1 charset back.
db = self._get_db()
charset = db.character_set_name()

if i check the charset variables with show command the system gives me utf8 charset for global and session variables as well. so, from where does come latin1?

any hint is appreciated!

this is my table:
DROP TABLE IF EXISTS `vss_history`.`unit`;
CREATE TABLE `vss_history`.`unit` (
`id` int(11) NOT NULL auto_increment,
`code` varchar(10) default NULL,

and this is my py code:
#-*- coding: utf-8 -*-
import MySQLdb

cnx = MySQLdb.connect(host="", user="root", passwd="XXXX", db="vss_history", charset='utf8')
c = cnx.cursor()

sb = u'รก'.encode('utf-8')

c.execute("insert into unit (code) values ('%s')" % sb)

this is the exception:
File "C:\Python24\Lib\site-packages\MySQLdb\", line 146, in execute
query = query.encode(charset)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 33: ordinal not in range(128)

thank you!

Options: ReplyQuote

Written By
insert unicode string
August 25, 2006 06:49AM
December 08, 2006 02:28AM
August 15, 2007 01:12PM
February 28, 2007 05:30PM

Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.