Rails 3,mysql / mysql2将一些检索的字符串曲解为ASCII-8BIT [英] Rails 3, mysql/mysql2 misinterpreting some retrieved strings as ASCII-8BIT
问题描述
这个问题开始于常见的不兼容的字符编码:ASCII-8BIT和UTF-8问题,但这是 不是 我在问什么。相反,我发现这个问题发生了,因为我们的数据库的某些字段在被检索时被标记为ASCII-8BIT,而大多数字段正确显示为UTF-8。
This problem began as the commonly-seen "incompatible character encodings: ASCII-8BIT and UTF-8" problem, but that is not what I'm asking. Rather, I discovered that this problem was happening because certain fields of my database are being tagged as ASCII-8BIT when they're retrieved, while most are correctly shown as UTF-8.
例如,在列为 country
和 nationality
,其中行16中的两列具有相同的值(复制和粘贴),我得到
For example, in a table with columns country
and nationality
, where both columns in row 16 have identical values (copied-and-pasted), I get
irb(main):003:0> c = Country.find(16)
irb(main):004:0> puts "#{c.name}, #{c.name.encoding}, #{c.name.bytes.to_a}"
�land Islands, UTF-8, [195, 133, 108, 97, 110, 100, 32, 73, 115, 108, 97, 110, 100, 115]
irb(main):005:0> puts "#{c.nationality}, #{c.nationality.encoding}, #{c.nationality.bytes.to_a}"
�land Islands, ASCII-8BIT, [195, 133, 108, 97, 110, 100, 32, 73, 115, 108, 97, 110, 100, 115]
,一个简单的放置名称
给出 land群岛
而国籍
它给出\xC3\x85land Islands
- 相同的字节,不同的演示文稿。
Likewise, a simple puts name
gives �land Islands
while for nationality
it gives "\xC3\x85land Islands"
-- same bytes, different presentation.
编码对于给定的列,无论该字符串是否具有非ASCII字符,似乎都是常数,因此不仅仅是字符串的问题。也就是说,所有 国籍
中的值被解释为名称中的ascii和全部
作为UTF-8。
The encoding for a given column appears to be constant regardless of whether the string has non-ascii characters, so it is not simply a problem with the string. That is, all the values in nationality
are interpreted as ascii and all those in name
as UTF-8.
问题不仅限于单个表,还没有找到任何列错误的模式,
The problem is not limited to a single table, and I have not found any pattern to which columns are mis-recognized.
以下是设置和环境:
- Rails 3.0.0在Windows 7 64位
- 数据库适配器:mysql2和mysql都显示相同的行为
- Database.yml包含
encoding: utf8
- application.rb include
config.encoding =utf-8
- MySQL数据库,表和两列都定义为utf8
- MySQL中的两列都是varchar,255,allow null
- 我可以使用新的Rails安装来重现问题,除了定义的国家模型之外,还可以访问数据库。我还没有尝试过一个新鲜的一行数据库。
- Rails 3.0.0 on Windows 7 64-bit
- Database adapter: mysql2 and mysql both show same behavior
- Database.yml includes
encoding: utf8
- application.rb includes
config.encoding = "utf-8"
- MySQL database, table, and both columns are defined as utf8
- Both columns in MySQL are varchar, 255, allow null
- I can reproduce the problem with a fresh installation of Rails and nothing except the Country model defined, to access the database. I have not yet tried with a fresh, one-line database.
任何人都知道这里发生了什么?
Anyone know what's going on here?
推荐答案
同样的问题在这里! mysql2 gem,Rails 3.0.3和不相容的字符编码错误
Same problem here! mysql2 gem, Rails 3.0.3 and "incompatible character encodings" errors
这篇关于Rails 3,mysql / mysql2将一些检索的字符串曲解为ASCII-8BIT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!