Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > unicode and mysql

Reply
Thread Tools

unicode and mysql

 
 
Ilya Knizhnik
Guest
Posts: n/a
 
      02-23-2004
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




 
Reply With Quote
 
 
 
 
Skip Montanaro
Guest
Posts: n/a
 
      02-23-2004

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

 
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
Python unicode utf-8 characters and MySQL unicode utf-8 characters Grzegorz Śliwiński Python 2 01-19-2011 07:31 AM
Strange unicode / no unicode phenomen with mysql Hans Müller Python 1 12-03-2009 10:06 PM
Help for Unicode char and Unicode char based string in Ruby Chirag Mistry Ruby 6 02-08-2008 12:45 PM
unicode and mysql Ilya Knizhnik Python 0 02-24-2004 03:14 AM
Unicode + jsp + mysql + tomcat = unicode still not displaying Robert Mark Bram Java 0 09-28-2003 05:37 AM



Advertisments