MySQL Forums
Forum List  »  Connector/Python

arabic text insertion problem
Posted by: hatem shaheen
Date: February 21, 2010 03:28AM

hi all,
I'm developing on linux using python 2.4.3 on centos 5.4

the attached code connect to the attached database animal.sql file
the problem occure when inserting arabic characters in the category form field
please see the error below

UnicodeDecodeError Python 2.4.3: /usr/bin/python
Sun Feb 21 11:38:07 2010

A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.
38 s=""" INSERT INTO animal (name, category) VALUES ('%s', '%s') """ % (name, category)
40 cursor.execute (s)
41 print "Number of rows inserted: %d" % cursor.rowcount
cursor = <MySQLdb.cursors.Cursor object>, cursor.execute = <bound method Cursor.execute of <MySQLdb.cursors.Cursor object>>, s = " INSERT INTO animal (name, category) VALUES ('a', '\xd8\xb6') "
/usr/lib/python2.4/site-packages/MySQLdb/ in execute(self=<MySQLdb.cursors.Cursor object>, query=" INSERT INTO animal (name, category) VALUES ('a', '\xd8\xb6') ", args=None)
144 db = self._get_db()
145 charset = db.character_set_name()
146 query = query.encode(charset)
147 if args is not None:
148 query = query % db.literal(args)
query = " INSERT INTO animal (name, category) VALUES ('a', '\xd8\xb6') ", query.encode = <built-in method encode of str object>, charset = 'latin1'

UnicodeDecodeError: 'ascii' codec can't decode byte 0xd8 in position 51: ordinal not in range(128)
args = ('ascii', " INSERT INTO animal (name, category) VALUES ('a', '\xd8\xb6') ", 51, 52, 'ordinal not in range(128)')
encoding = 'ascii'
end = 52
object = " INSERT INTO animal (name, category) VALUES ('a', '\xd8\xb6') "
reason = 'ordinal not in range(128)'
start = 51

can any body help in this problem solving
Best Regards
hatem gamal

source code files are here
import sys
import MySQLdb
import string
import codecs
import cgi
import cgitb

form = cgi.FieldStorage()

if form:
name = form['name'].value

print "content-type: text/html;charset=utf-8\n\n"
print """<HTML><HEAD><TITLE>
Friends CGI Demo (dynamic screen)

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

if status =="insert":
cursor = conn.cursor ()
s=u""" INSERT INTO animal (name, category) VALUES ('%s', '%s') """ % (name, unicode(category))
cursor.execute (s)
print "Number of rows inserted: %d" % cursor.rowcount

print """
<form name="n" action="?" method="post">
<tr><td>Your name is:</td> <td><input type="text" name="name"/></td><tr>
<tr><td>Your category is:</td> <td> <input type="text" name="category"/> </td><tr>
<tr><td><input type="submit"/></td> <td>&nbsp;</td></tr>

cursor = conn.cursor ()
cursor.execute ("SELECT name, category FROM animal")
result_set = cursor.fetchall ()

defining indicies
name_i= 0
category_i= 1

loop to display
print """<table cellspacing="2" cellpading="0" border="0"><tr><th>%s</th><th>%s</th></tr>""" % ("الاسم /name"," الفئة /category")
for row in result_set:
print "<tr><td>%s</td><td>%s</td><td>تعديل</td><td>حذف</td></tr>" % (row[name_i], row[category_i])
print "</table>"
print "Number of rows returned: %d <br />" % cursor.rowcount
cursor.close ()
conn.commit ()
conn.close ()

`name` char(40) default NULL,
`category` char(40) character set utf8 collate utf8_unicode_ci default NULL

-- Dumping data for table `animal`

INSERT INTO `animal` (`name`, `category`) VALUES
('hatem', 'عبد'),
('atif', 'عاطف'),
('hatem', 'عبد'),
('hatem', 'عبد'),
('hatem', 'عبد'),
('hatem', 'عبد'),
('hatem', 'عبد'),
('حاتم', 'عبد'),
('dd', 'xd8xaaxd8xaaxd8xaa'),
('aaa', 'ccc'),
('كككك', 'ممم'),
('g', 'c');


Options: ReplyQuote

Written By
arabic text insertion problem
February 21, 2010 03:28AM

Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.