MySQL Forums
Forum List  »  Connector/Python

Re: Inserting unicode strings
Posted by: Geert Vanderkelen
Date: December 02, 2009 02:13AM

Example table:
CREATE TABLE `unicode` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`c1` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8


Python code:

# start python code
import MySQLdb
db = MySQLdb.connect(db='test',user='root',charset='utf8')
c = db.cursor()
zen = u'\u305c\u3093'
c.execute("INSERT INTO unicode (c1) VALUES ('%s')" % (zen,))
c.close()
db.close()
# end python code

Result after executing the above:

mysql> SET NAMES 'utf8';
mysql> SELECT * FROM unicode;
+----+--------+
| id | c1 |
+----+--------+
| 1 | ぜん |
+----+--------+

More about Python and Unicode here:
http://boodebr.org/main/python/all-about-python-and-unicode

Cheers,

Geert

Geert Vanderkelen
Software Developer at Oracle

Options: ReplyQuote


Subject
Written By
Posted
Re: Inserting unicode strings
December 02, 2009 02:13AM


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.