Quantcast
Channel: MySQL Forums - Connector/Python
Viewing all articles
Browse latest Browse all 384

arabic text insertion problem (3 replies)

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

the attached code db05.py 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.
/var/www/cgi-bin/db05.py
38 s=""" INSERT INTO animal (name, category) VALUES ('%s', '%s') """ % (name, category)
39
40 cursor.execute (s)
41 print "Number of rows inserted: %d" % cursor.rowcount
42
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/cursors.py 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
======================
db05.py
======================
#!/usr/bin/python
import sys
import MySQLdb
import string
import codecs
import cgi
import cgitb
cgitb.enable()

form = cgi.FieldStorage()

if form:
status="insert"
name = form['name'].value
category=form['category'].value
else:
status="display"

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

try:
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 """
<table>
<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>
</form>
</table>
"""


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
print"""</BODY></HTML>"""
cursor.close ()
conn.commit ()
conn.close ()

======================
animal.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', 'عبد'),
('hatem', 'عبد'),
('hatem', 'عبد'),
('hatem', 'عبد'),
('hatem', 'عبد'),
('حاتم', 'عبد'),
('dd', 'xd8xaaxd8xaaxd8xaa'),
('aaa', 'ccc'),
('كككك', 'ممم'),
('g', 'c');

======================

Viewing all articles
Browse latest Browse all 384

Trending Articles