MySQLdb problem

L

liupei

I am use mod_python3.2.8,MySQL-python-1.2.1_p2,mysql5.0.20,centOS
when I run the script below(I have also saved this script in utf-8):
#coding: utf-8
from MySQLdb import connect
connection=connect(user='root',passwd='',host='localhost',db='test')
cursor = connection.cursor()
cursor.execute("INSERT INTO `fee` ( `affair` ) VALUES ('测试')")
#'测试' is a chinese word means test

it raise error:
Traceback (most recent call last):
File "test.py", line 8, in ?
cursor.execute("INSERT INTO `fee` ( `affair` ) VALUES ('测试')")
File "/opt/python/lib/python2.4/site-packages/MySQLdb/cursors.py",
line 146, in execute
query = query.encode(charset)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position
40: ordinal not in range(128)
 
D

Dennis Lee Bieber

I am use mod_python3.2.8,MySQL-python-1.2.1_p2,mysql5.0.20,centOS
when I run the script below(I have also saved this script in utf-8):
#coding: utf-8
from MySQLdb import connect
connection=connect(user='root',passwd='',host='localhost',db='test')
cursor = connection.cursor()
cursor.execute("INSERT INTO `fee` ( `affair` ) VALUES ('??')")
#'??' is a chinese word means test
Well... First off... Don't embed the values into the query string,
let the DB adapter do that for you...

cursor.execute("insert into fee (affair) values (%s)", ("??",) )

Second matter -- hypothesis -- you may need to encode the ?? value
before passing it to the query; check the MySQL documents for how it
handles unicode.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top