In my experience ASCII strings get into MySQL okay. Strings containing
non-ASCII characters either cause an error or a warning. Here is a way
to reproduce the UnicodeDecodeError.

I am using:
mysql  Ver 14.12 Distrib 5.0.22, for pc-linux-gnu (i486) using readline 5.1
Python 2.4.3

# Script to create database with InnoDB table with UTF-8 collation

create database leks;

grant select, update, insert, delete on leks.* to ETC...

use leks;

create table leksiko (
teksto_id SMALLINT(4) UNSIGNED NOT NULL AUTO_INCREMENT,
vorto varchar(60) not null,
word  varchar(60) not null,
tipo  varchar(2)  not null,
laste_ŝanĝita TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (teksto_id)
) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_esperanto_ci;


# Python script bug57067.py

#!/usr/bin/python
# -*- coding: utf-8 -*-

import connect_leks

try:
    conn = connect_leks.connect()
    cursor = conn.cursor()
    sql = u'INSERT INTO leksiko (vorto, word, tipo) VALUES ("havi", "have", 
"i")'
    cursor.execute(sql) # insert into database
    sql = u'INSERT INTO leksiko (vorto, word, tipo) VALUES ("ŝanĝi", "change", 
"i")'
    cursor.execute(sql) # insert into database
    conn.commit()
finally:
    conn.close()


# On running Python script in terminal:

Connected to leks
Traceback (most recent call last):
  File "./bug57067.py", line 17, in ?
    cursor.execute(sql) # insert into database
  File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 137, in 
execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line 33, in 
defaulterrorhandler
    raise errorclass, errorvalue
UnicodeEncodeError: 'latin-1' codec can't encode character u'\u015d' in 
position 49: ordinal not in range(256)

# mysql shows that the second record has not been inserted into
database:

mysql> select * from leksiko order by vorto;
+-----------+-------+------+------+---------------------+
| teksto_id | vorto | word | tipo | laste_ŝanĝita     |
+-----------+-------+------+------+---------------------+
|      5807 | havi  | have | i    | 2006-10-20 13:34:18 |
+-----------+-------+------+------+---------------------+
1 row in set (0.00 sec)

### I hope this helps.
### Donald Rogers

-- 
UnicodeDecodeError: 'ascii' codec can't decode certain bytes 
https://launchpad.net/bugs/57067

-- 
ubuntu-bugs mailing list
ubuntu-bugs@lists.ubuntu.com
https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs

Reply via email to