MySQLDB - return strange type of variable

Jeffrey Froman jeffrey at fro.man
Sat Mar 25 11:20:28 EST 2006


Grzegorz Smith wrote:

> Hi all. I'm trying get data from text field in MySQl 5.0 with my National
> characters. Data are stored in utf8 encodings. Here is the script:
> import MySQLdb, MySQLdb.cursors
> conn = MySQLdb.connect(host='localhost', user='root', passwd='123456',
> db='profile_locale')
> c = conn.cursor(MySQLdb.cursors.DictCursor)
> c.execute("SET CHARACTER SET utf8")
> c.execute("SELECT string_value FROM lang_pl_pl WHERE id=8")
> row = c.fetchone()
> print row
> and i get:
> {'string_value': array('c', 'Zmie\xc5\x84 has\xc5\x82o')}
> where it come array type?

Recent versions of MySQL/MySQLdb return "binary strings" as Python array
objects, rather than bytestrings (I think the change was around version 4.2
of MySQL.) Details on array objects are here:

http://www.python.org/doc/lib/module-array


> How can i get value 'Zmie\xc5\x84 has\xc5\x82o' ??

array('c', 'Zmie\xc5\x84 has\xc5\x82o').tostring()


> If I do c.fetchone -shouldn't i get type tuple? 

If you use the default MySQLdb cursor object you will get a tuple. Using the
DictCursor as you are returns a dictionary instead.


Jeffrey





More information about the Python-list mailing list