Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > MySQLdb problem

Reply
Thread Tools

MySQLdb problem

 
 
liupei
Guest
Posts: n/a
 
      05-07-2006
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-:
#coding: utf-8
from MySQLdb import connect
connection=connect(user='root',passwd='',host='loc alhost',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(12

 
Reply With Quote
 
 
 
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      05-07-2006
On 6 May 2006 18:24:59 -0700, "liupei" <(E-Mail Removed)> declaimed
the following in comp.lang.python:

> 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-:
> #coding: utf-8
> from MySQLdb import connect
> connection=connect(user='root',passwd='',host='loc alhost',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
http://www.velocityreviews.com/forums/(E-Mail Removed) (E-Mail Removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (E-Mail Removed))
HTTP://www.bestiaria.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQLdb problem with mod_python, please help ws Wang Python 2 11-30-2004 08:40 PM
Newbie MySQLdb / MySQL version problem, I think Dave Merrill Python 7 11-29-2004 05:23 PM
Nagging problem with MySQLdb Phillip Python 5 11-24-2004 01:56 AM
problem installing MySQLdb on Solaris 8 Bob Swerdlow Python 0 05-26-2004 03:53 PM
Re: Problem with MySQLdb on Mac OS X... Skip Montanaro Python 5 07-16-2003 04:46 AM



Advertisments