mysql unicode error

hi all,
I have database unicode error that I don't know why it happen
below db.sql the database file
and db02.py the file that insert into database
and db04.py the file that retrive from database
I can't send unicode character to database until I package the sql
statment with u'' operator so it looks like the following u""" INSERT
INTO animal (name, category) VALUES ('hatem', 'عبد') """
but when retriving it normally as follows
print "name : %s, category : %s" % (row[0], row[1])
it return garbage like this
name : hatem, category : عبد
and when using decode function like this
print u"name : %s, category : %s" % (row[0], string.decode(row[1]))
I get this error
[shang1@localhost ~]$ '/data/python/db04.py'
Traceback (most recent call last):
  File "/data/python/db04.py", line 26, in ?
    print u"name : %s, category : %s" % (row[0],
string.decode(row[1]))
AttributeError: 'module' object has no attribute 'decode'

my question is :
can any body help in that
the following are code sample I use
thanks in advance
best regards
hg

···

====================
db.sql

CREATE TABLE IF NOT EXISTS `animal` (
  `name` char(40) default NULL,
  `category` char(40) character set utf8 collate utf8_unicode_ci
default NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `animal`
--

INSERT INTO `animal` (`name`, `category`) VALUES
('hatem', 'عبد'),
('atif', 'عاطف'),
('hatem', 'عبد');

====================
db02.py

#!/usr/bin/python
   # animal.py - create animal table and
   # retrieve information from it

import sys
import MySQLdb

try:
     conn = MySQLdb.connect (host = "localhost",
                             user = "root",
                             passwd = "",
                             db = "py",
                             charset = "utf8",
                             use_unicode = True)
except MySQLdb.Error, e:
     print "Error %d: %s" % (e.args[0], e.args[1])
     sys.exit (1)

cursor = conn.cursor ()
cursor.execute (u""" INSERT INTO animal (name, category) VALUES
('hatem', 'عبد') """ )
print "Number of rows inserted: %d" % cursor.rowcount

============
db04.py

#!/usr/bin/python
   # animal.py - create animal table and
   # retrieve information from it

import sys
import MySQLdb
import string

try:
     conn = MySQLdb.connect (host = "localhost",
                             user = "root",
                             passwd = "",
                             db = "py",
                             charset = "utf8",
                             use_unicode = True)
except MySQLdb.Error, e:
     print "Error %d: %s" % (e.args[0], e.args[1])
     sys.exit (1)

cursor = conn.cursor ()
cursor.execute ("SELECT name, category FROM animal")
while (1):
     row = cursor.fetchone ()
     if row == None:
       break
     print u"name : %s, category : %s" % (row[0],
string.decode(row[1]))
print "Number of rows returned: %d" % cursor.rowcount

I'm not sure how this relates to wxPython. Try re-posting to the
Python mailing list.

- Mike

···

On Feb 10, 7:12 am, shang <shang5...@gmail.com> wrote:

hi all,
I have database unicode error that I don't know why it happen
below db.sql the database file
and db02.py the file that insert into database
and db04.py the file that retrive from database
I can't send unicode character to database until I package the sql
statment with u'' operator so it looks like the following u""" INSERT
INTO animal (name, category) VALUES ('hatem', 'عبد') """
but when retriving it normally as follows
print "name : %s, category : %s" % (row[0], row[1])
it return garbage like this
name : hatem, category : عبد
and when using decode function like this
print u"name : %s, category : %s" % (row[0], string.decode(row[1]))
I get this error
[shang1@localhost ~]$ '/data/python/db04.py'
Traceback (most recent call last):
File "/data/python/db04.py", line 26, in ?
print u"name : %s, category : %s" % (row[0],
string.decode(row[1]))
AttributeError: 'module' object has no attribute 'decode'

my question is :
can any body help in that
the following are code sample I use
thanks in advance
best regards
hg

====================
db.sql

CREATE TABLE IF NOT EXISTS `animal` (
`name` char(40) default NULL,
`category` char(40) character set utf8 collate utf8_unicode_ci
default NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `animal`
--

INSERT INTO `animal` (`name`, `category`) VALUES
('hatem', 'عبد'),
('atif', 'عاطف'),
('hatem', 'عبد');

====================
db02.py

#!/usr/bin/python
# animal.py - create animal table and
# retrieve information from it

import sys
import MySQLdb

try:
conn = MySQLdb.connect (host = "localhost",
user = "root",
passwd = "",
db = "py",
charset = "utf8",
use_unicode = True)
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit (1)

cursor = conn.cursor ()
cursor.execute (u""" INSERT INTO animal (name, category) VALUES
('hatem', 'عبد') """ )
print "Number of rows inserted: %d" % cursor.rowcount

============
db04.py

#!/usr/bin/python
# animal.py - create animal table and
# retrieve information from it

import sys
import MySQLdb
import string

try:
conn = MySQLdb.connect (host = "localhost",
user = "root",
passwd = "",
db = "py",
charset = "utf8",
use_unicode = True)
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit (1)

cursor = conn.cursor ()
cursor.execute ("SELECT name, category FROM animal")
while (1):
row = cursor.fetchone ()
if row == None:
break
print u"name : %s, category : %s" % (row[0],
string.decode(row[1]))
print "Number of rows returned: %d" % cursor.rowcount

Hi,

and when using decode function like this
print u"name : %s, category : %s" % (row[0], string.decode(row[1]))
I get this error
[shang1@localhost ~]$ '/data/python/db04.py'
Traceback (most recent call last):
File "/data/python/db04.py", line 26, in ?
print u"name : %s, category : %s" % (row[0],
string.decode(row[1]))
AttributeError: 'module' object has no attribute 'decode'

This has nothing to do with wxPython, but did you read what this
errors says? It holds all the answers you could ever need or want;)

In your code you import the python module 'string', the error above
says that there is no decode method in the string module. Which is
true. String objects have a decode method. You probably want to call
row[1].decode(encoding='whatever_encoding_your_using').

Cody

···

On Wed, Feb 10, 2010 at 7:12 AM, shang <shang5000@gmail.com> wrote:

thanks man

···

On Wed, Feb 10, 2010 at 8:51 PM, Cody Precord codyprecord@gmail.com wrote:

Hi,

On Wed, Feb 10, 2010 at 7:12 AM, shang shang5000@gmail.com wrote:

and when using decode function like this

print u"name : %s, category : %s" % (row[0], string.decode(row[1]))

I get this error

[shang1@localhost ~]$ ‘/data/python/db04.py’

Traceback (most recent call last):

File “/data/python/db04.py”, line 26, in ?

print u"name : %s, category : %s" % (row[0],

string.decode(row[1]))

AttributeError: ‘module’ object has no attribute ‘decode’

This has nothing to do with wxPython, but did you read what this

errors says? It holds all the answers you could ever need or want;)

In your code you import the python module ‘string’, the error above

says that there is no decode method in the string module. Which is

true. String objects have a decode method. You probably want to call

row[1].decode(encoding=‘whatever_encoding_your_using’).

Cody

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en