Solved: Inserting Unicode text with MySQLdb in Python 2.4-2.5?

Threader Slash threaderslash at gmail.com
Wed Nov 18 17:07:01 EST 2009


---------- Forwarded message ----------
From: Keith Hughitt <keith.hughitt at gmail.com>
To: python-list at python.org
Date: Wed, 18 Nov 2009 06:09:11 -0800 (PST)
Subject: Inserting Unicode text with MySQLdb in Python 2.4-2.5?
Hi all,

I ran into a problem recently when trying to add support for earlier
versions of Python (2.4 and 2.5) to some database related code which
uses MySQLdb, and was wondering if anyone has any suggestions.

With later versions of Python (2.6), inserting Unicode is very simple,
e.g.:

   # -*- coding: utf-8 -*-
   ...
   cursor.execute('''INSERT INTO `table` VALUES (0,
'Ångström'),...''')

When the same code is run on earlier versions, however, the results is
either garbled text (e.g. "Ã or "?" instead of "Å" in Python 2.5), or
an exception being thrown (Python 2.4):

   UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in
position 60: ordinal not in range(128)

So far I've tried a number of different things, including:

   1. Using Unicode strings (e.g. u"\u212B")

   2. Manually specifying the encoding using sys.setdefaultencoding
('utf-8')

   3. Manually enabling Unicode support in MySQLdb
(use_unicode=False, charset = "utf8")

...but no combination of any of the above resulted in proper database
content.

To be certain that the issue was related to Python/MySQLdb and not
MySQL itself, I manually inserted the text and it worked just fine.
Furthermore, when working in a Python console, both print "Å" and
print u"\u212B" display the correct output.

Any ideas? The versions of the MySQLdb adapter tested were 1.2.1
(Python 2.4), and 1.2.2-10 (Python 2.5).

Thanks!
Keith
----

Hello Keithm,

Here is your answer...

I run on Python2.5, this syntax works fine for me -- a piece of my code:

        getMaxID_Query = """SELECT MAX(customerID)
        FROM customer;"""

        self.cursorMySQL.execute(getMaxID_Query)

Try to use: """ instead of ''' . It will solve your problem.

ThreaderSlash
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091119/64b88947/attachment.html>


More information about the Python-list mailing list