Rails 3、mysql/mysql2 将某些检索到的字符串误解为 ASCII-8BIT
这个问题最初是常见的“不兼容的字符编码:ASCII-8BIT 和 UTF-8”问题,但这不是我要问的。相反,我发现发生此问题是因为数据库的某些字段在检索时被标记为 ASCII-8BIT,而大多数字段正确显示为 UTF-8。
例如,在包含 country
和 nationality
列的表中,第 16 行中的两列具有相同的值(复制并粘贴),我得到
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]
同样,一个简单的 < code>puts name 给出 �landIslands
而对于 nationality
它给出 "\xC3\x85landIslands"
-- 相同的字节,不同的演示。
无论字符串是否包含非 ascii 字符,给定列的编码似乎都是恒定的,因此这不仅仅是字符串的问题。也就是说,nationality
中的所有值都被解释为 ascii,name
中的所有值都被解释为 UTF-8。
问题不仅限于单个表,而且我还没有发现任何列被错误识别的模式。
以下是设置和环境:
- Windows 7 64 位上的 Rails 3.0.0
- 数据库适配器:mysql2 和 mysql 都显示相同的行为
- Database.yml 包含
encoding: utf8
- application.rb 包含
config。 coding = "utf-8"
- MySQL 数据库、表和两列都定义为 utf8
- MySQL 中的两列都是 varchar, 255, allowed null
- 我可以通过全新安装 Rails 重现该问题,除了国家/地区之外什么都没有定义模型,访问数据库。我还没有尝试过使用新的单行数据库。
有人知道这是怎么回事吗?
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.
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]
Likewise, a simple puts name
gives �land Islands
while for nationality
it gives "\xC3\x85land Islands"
-- same bytes, different presentation.
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.
Here are the settings and environment:
- 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里同样的问题! mysql2 gem,Rails 3.0.3 和“不兼容”字符编码”错误
Same problem here! mysql2 gem, Rails 3.0.3 and "incompatible character encodings" errors
我找到了解决方案,使用 ruby-mysql gem 而不是 mysql 或 mysql2 gems。
I found the solution, use ruby-mysql gem instead mysql or mysql2 gems.
我想我也面临着类似的问题。
当我从 MySQL DB Rails 检索数据时,将字符串转换为浮点数:
列“Complemento”和“estado”是数据库中的字符串,尽管操作“show”说
补数:0.0 -> DB 为“apto 191”
Escola 已成功创建。
名称:Silva Braga
Endereco:Rua Dr Arnaldo
编号:99
补码:0.0 -> DB 为“apto 191”
Cidade:圣保罗
州:0.0 -> DB 是“MG”
编辑 |后退
I think I'm living a similar problem.
When I retrivied datas from MySQL DB rails convert a string to float:
Columns "Complemento" and "estado" are string in DB although action "show" says
Complemento: 0.0 -> at DB is "apto 191"
Escola was successfully created.
Nome: Silva Braga
Endereco: Rua Dr Arnaldo
Numero: 99
Complemento: 0.0 -> DB is "apto 191"
Cidade: São Paulo
Estado: 0.0 -> DB is "MG"
Edit | Back