MySQLdb problem

Discussion in 'Python' started by liupei, May 7, 2006.

  1. liupei

    liupei Guest

    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)
     
    liupei, May 7, 2006
    #1
    1. Advertising

  2. On 6 May 2006 18:24:59 -0700, "liupei" <> 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-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

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, May 7, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Skip Montanaro

    Re: Problem with MySQLdb on Mac OS X...

    Skip Montanaro, Jul 12, 2003, in forum: Python
    Replies:
    5
    Views:
    393
    Michael S. Jessop
    Jul 16, 2003
  2. Bob Swerdlow

    problem installing MySQLdb on Solaris 8

    Bob Swerdlow, May 26, 2004, in forum: Python
    Replies:
    0
    Views:
    451
    Bob Swerdlow
    May 26, 2004
  3. Phillip

    Nagging problem with MySQLdb

    Phillip, Nov 22, 2004, in forum: Python
    Replies:
    5
    Views:
    369
    Damjan
    Nov 24, 2004
  4. ws Wang
    Replies:
    2
    Views:
    492
    ws Wang
    Nov 30, 2004
  5. Dave Merrill
    Replies:
    7
    Views:
    436
    Dave Merrill
    Nov 29, 2004
Loading...

Share This Page