MySQLdb

S

slukas

Hello
Sorry for my English.
I want add the new record to Mysql database. When I use my script I got
an exception "UnicodeEncodeError: 'ascii' codec can't encode character
u'\xe4' in position 12: ordinal not in range(128).
When I add line
sys.stdout = codecs.lookup('utf-8')[-1](sys.stdout)
to my script i see the var on the screen but I can't add var to database.

Any idea?
Thanks!
Lukas
 
S

Skip Montanaro

Lukas> I want add the new record to Mysql database. When I use my script
Lukas> I got an exception "UnicodeEncodeError: 'ascii' codec can't
Lukas> encode character u'\xe4' in position 12: ordinal not in
Lukas> range(128).

Lukas,

You will need to encode your Unicode objects before stuffing the resulting
strings into MySQL. For example:

utf8foo = foo.encode("utf-8")
c.execute("insert into blah blah blah", (..., utf8foo, ...))

instead of

c.execute("insert into blah blah blah", (..., foo, ...))

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

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top