unicode and mysql

Discussion in 'Python' started by Ilya Knizhnik, Feb 23, 2004.

  1. Hi all,

    I'm having a problem inserting into a mysql database (its not the mysql, its
    the encoding of the query sting), could someone point me in the
    right direction. The code resembles the following:

    text1=hdtext.encode("utf-8") #this is acquired earlier in the script
    string1="just testing"

    #this first one would work fine
    cursor.execute("insert into table1 (col1) values (%s)" %(text1))
    #the next one works too
    cursor.execute("insert into table1 (col2) values ('%s')" %(string1))

    #however what i need to work is the following
    cursor.execute("insert into table1 (col1,col2) values (%s,'%s')"
    % (text1,string1)


    Thanks,


    Ilya
    Ilya Knizhnik, Feb 23, 2004
    #1
    1. Advertising

  2. Ilya> I'm having a problem inserting into a mysql database (its not the
    Ilya> mysql, its the encoding of the query sting), could someone point
    Ilya> me in the right direction. The code resembles the following:

    Ilya> text1=hdtext.encode("utf-8") #this is acquired earlier in the script
    Ilya> string1="just testing"

    Ilya> #this first one would work fine
    Ilya> cursor.execute("insert into table1 (col1) values (%s)" %(text1))
    Ilya> #the next one works too
    Ilya> cursor.execute("insert into table1 (col2) values ('%s')" %(string1))

    Ilya> #however what i need to work is the following
    Ilya> cursor.execute("insert into table1 (col1,col2) values (%s,'%s')"
    Ilya> % (text1,string1)

    Let your database module (presumably MySQLdb) do the encoding for you:

    cursor.execute("insert into table1 (col1,col2) values (%s, %s)",
    (text1, string1))

    Skip
    Skip Montanaro, Feb 23, 2004
    #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. Robert Mark Bram
    Replies:
    0
    Views:
    3,909
    Robert Mark Bram
    Sep 28, 2003
  2. Jeffrey H. Coffield
    Replies:
    1
    Views:
    1,829
  3. Roedy Green
    Replies:
    0
    Views:
    1,462
    Roedy Green
    Jul 23, 2009
  4. Hans Müller
    Replies:
    1
    Views:
    277
    Hans Müller
    Dec 3, 2009
  5. Grzegorz ¦liwiñski
    Replies:
    2
    Views:
    939
    Grzegorz ¦liwiñski
    Jan 19, 2011
Loading...

Share This Page