Inserting Unicode text with MySQLdb in Python 2.4-2.5?
Keith Hughitt
keith.hughitt at gmail.com
Thu Nov 19 10:48:16 EST 2009
Hello,
Thanks for the suggestions and information Diez!
On Nov 18, 9:38 am, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
>
> You are aware that the coding-declaration only affects unicode-literals (the
> ones like u"i'm unicode")? So the above insert-statement is *not* unicode,
> it's a byte-string in whatever encoding your editor happens to save the
> file in.
Thanks. I didn't know that, but that is helpful.
>
> And that's point two: make sure your editor reads and writes the file in the
> same encoding you specified in the comment in the beginning.
That is taken care of: the files are opened/saved as UTF-8.
>
> Makes sense if the execute tries to encode to unicode first - as you didn't
> give it a unicode-object.
>
In Python 2.6 where I originally developed the code, it wasn't
necessary to explicitly specify the type of
some text: it was basically handled for you. It does make sense
though.
>
>
> > 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")
>
> You *disabled* unicode here!!!!! Unicode is NOT utf-8!!!
Oops. It was enabled when I ran it, I just copied the above text from
somewhere else and forgot to change it. I am aware that Unicode does
not equal
utf-8, but utf-8 is a Unicode encoding, right?
>
> http://www.joelonsoftware.com/articles/Unicode.html
Thanks!
>
> Try the above, and better yet provide self-contained examples that show the
> behavior.
>
> Diez
Still no luck. I also tried using double-quotes instead of single-
quotes around the relevant strings (as suggested over email by
ThreaderSlash), however, that did
not work for me.
Here is a small example of what I'm trying to do. Notice that if you
run it in Python 2.5-2.6, everything works fine. It is only in Python
2.4 that the
below example doesn't work.
============= Begin Example ==================
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import sys
def main():
import MySQLdb, getpass
admin = raw_input("Database admin: ")
pw = getpass.getpass("Password: ")
db = MySQLdb.connect(user=admin, passwd=pw)
cursor = db.cursor()
cursor.execute("CREATE DATABASE IF NOT EXISTS unicode_test;")
cursor.execute('''
CREATE TABLE `unicode_test`.`test` (
`id` SMALLINT unsigned NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`), INDEX (`id`)
) DEFAULT CHARSET=utf8;''')
cursor.execute('''
INSERT INTO `unicode_test`.`test` VALUES
(NULL, 'Ångström');
''')
# Test 1
print "Just printing: %s" % 'Ångström'
# Test 2
cursor.execute("SELECT name FROM unicode_test.test;")
print "From database: %s" % cursor.fetchone()[0].decode('utf-8')
# Test 3 (Manual)
print 'To verify manually: mysql -u %s -p -e "SELECT name FROM
unicode_test.test"' % admin
if __name__ == '__main__':
sys.exit(main())
============= End Example ====================
Any suggestions?
Thanks!
Keith
More information about the Python-list
mailing list