Unicode from Web to MySQL

Serge Orlov sombDELETE at pobox.ru
Sun Dec 21 06:15:05 EST 2003


> . What's driving me crazy is knowing the encoding
> but still not getting the data all the way through the chain to MySQL.

That's because you're trying to create SQL statements manually.

>>>2) Convert to unicode before UTF-8
>>Not sure what that means.
> data.decode(None,'strict')
> or
> unicode(data,'unicode','strict')

See Francis' excellent post why both of these calls do not make sence at all.

>>>3) replace quotation marks within the SQL statement:
>>>data2.replace(u'"',u'\\"')
>>It's not a unicode problem, is it?
> Occasionally instead of getting the encoding error I get a SQL syntax error,
> and figured somewhere it was misinterpreting something like the end
> delimiter.
> No proof though, just a guess, so I tried the replaces.

Uh, I see. If you're creating SQL statement yourself, you have to take care
of escaping. I guess after data2.encode('utf-8') you've got illegal (in SQL)
characters and you have to escape them. Another quick scan of mysql-python
docs reveals that .execute method can be called as
c.execute(''' INSERT INTO junk ( junklet) VALUES ( '%s') ''', args=(data2,) )
where data2 is a unicode string. Then the python wrapper will take care of
unicode convertions and escaping. Don't forget that you need to .connect with
unicode='utf-8' parameter.

-- Serge.






More information about the Python-list mailing list