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)