unicode and mysql

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ilya Knizhnik

    unicode and mysql

    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.en code("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




  • Skip Montanaro

    #2
    Re: unicode and mysql


    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.en code("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

    Comment

    Working...