开发者

Unicode formatting lost when inserting into MySQL database in Ruby 1.8.7

In Ruby, I have a bunch of strings encoded in UTF-8, e.g.: "HEC Montr\u00e9al".

When I insert it开发者_如何学Python into my MySQL table (formatted as utf8_general_ci) using the 'mysql' gem, the backslash is removed. What gives :) ? Any of you have any idea what the heck is going on here?

edit:

example string:

>> p mystring
"HEC Montr\\u00e9al"

and in the database after insert:

HEC Montru00e9al


This is not UTF:

'HEC Montr\u00e9al'

That's an ASCII representation of a JSON-encoded Unicode string. If it was UTF-8, it would look like:

'HEC Montréal'

You're not properly decoding your JSON inputs somewhere or your client-side code is sending your server JSON when your server is expecting plain text.

First you need to figure out why you're getting JSON encoded strings when you're not expecting them or figure out why you're not properly decoding your JSON. Then you can see if the database is mangling your UTF-8.


I believe you have to explicitly tell the MySQL gem to expect utf8. Something like this:

db = Mysql.init
db.options(Mysql::SET_CHARSET_NAME, 'utf8')
db.real_connect(...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜