unicode and mysql

I

Ilya Knizhnik

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
 
S

Skip Montanaro

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
 

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
474,183
Messages
2,570,968
Members
47,518
Latest member
TobiasAxf

Latest Threads

Top