本文介绍了UTF8的Rails问题 - utf8_general_ci的编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在这个特定的网站上,我所有的表都使用 utf8 / utf8_general_ci 编码。



在该数据库中,我有一些数据如下所示:

  mysql> select * from currency_types limit 1,10; 
+ ------ + ----------------- + --------- +
|代码|名称|符号|
+ ------ + ----------------- + --------- +
| CAD |加拿大元| $ |
| CNY |人民币| å...ƒ|
| EUR |欧元| â,¬|
| GBP |磅| £|
| INR |印度卢比| â,¨|
|日元|日元Â¥|
| MXN |墨西哥比索| $ |
| USD |美元| $ |
| PHP |菲律宾比索| â,±|
| DKK |丹麦克朗kr |
+ ------ + ----------------- + --------- +

以下是我遇到的问题



(在debian框上运行db和Rails站点),从Rails显示符号的字符正确显示。例如,中国人民币在我的浏览器中显示为元素,而不是数据库中显示的元素。



当我将该数据下载到我的本地OS X开发机器并在本地运行db和Rails,我在浏览器上看到DB(å...ƒ)内的表示,而不是我在分段中看到的字符元。



调试我已经做了



我已经确保Content-Type的所有标题都会从每个网络服务器(本地,



我的本​​地mysql服务器和登台服务器都设置为使用utf8作为默认字符集。我在使用set namesutf8之前,我进行任何调用。



我甚至可以从我的OS X Rails主机连接到我的分段数据库,而我还是看人物å...ƒ代表人民币。我猜测了,也许我的mysql本地客户端有一个问题,但是我无法弄清楚问题是什么。



也许这可能会借用一个线索



为了使它更加混乱,如果我将字符元素粘贴到本地机器上的数据库,我看到在网络浏​​览器中。 --- YET如果我把同一个字符粘贴到我的分段数据库中,我得到一个?标记在我的分段Rails站点的页面上。



另外,在我的OS X rails机器上,如果我在查询之前使用set nameslatin1 ,角色都回来了。我以前把这些表设置为latin1 - 这可能是问题吗?



有人请帮助我在这里,我会疯了,想弄清楚有什么问题!

解决方案

AHA!似乎有一些表信息在latin1中编码,愚蠢地将数据库更改为utf8而不进行转换。



运行以下操作修正了currency_types表:

  mysqldump -u root -p --opt --default-character-set = latin1 --skip-set-charset DBNAME> DBNAME.sql 

mysql -u root -p --default-character-set = utf8 DBNAME< DBNAME.sql

现在我只需要确保在latin1> utf8开关后生成的其他内容是不好意思:(


I have a staging Rails site up that's running on MySQL 5.0.32-Debian.

On this particular site, all of my tables are using utf8 / utf8_general_ci encoding.

Inside that database, I have some data that looks like so:

mysql> select * from currency_types limit 1,10;
+------+-----------------+---------+
| code | name            | symbol  |
+------+-----------------+---------+
| CAD  | Canadian Dollar | $       |
| CNY  | Chinese Yuan    | å…ƒ     |
| EUR  | Euro            | €     |
| GBP  | Pound           | £      |
| INR  | Indian Rupees   | ₨     |
| JPY  | Yen             | ¥      |
| MXN  | Mexican Peso    | $       |
| USD  | US Dollar       | $       |
| PHP  | Philippine Peso | ₱     |
| DKK  | Denmark Kroner  | kr      |
+------+-----------------+---------+

Here's the issue I'm having

On staging (with the db and Rails site running on the debian box), the characters for symbols are appearing correctly when displayed from Rails. For instance, the Chinese Yuan is appearing as 元 in my browser, not å…ƒ as it shows inside the database.

When I download that data to my local OS X development machine and run the db and Rails locally, I see the representation from inside the DB (å…ƒ) on my browser, not the character 元 as I see in staging.

Debugging I've done

I've ensured all headers for Content-Type are coming back as utf8 from each webserver (local, staging).

My local mysql server and the staging server are both setup to use utf8 as the default charset. I'm using "set names 'utf8'" before I make any calls.

I can even connect to my staging db from my OS X Rails host, and I still see the characters å…ƒ representing the yuan. I'm guessing then, perhaps there's an issue with my mysql local client, but I can't figure out what the issue is.

Perhaps this might lend a clue

To make it even more confusing, if I paste the character 元 into the db on my local machine, I see that in the web browser fine. --- YET if I paste that same character into my staging db, I get a ? mark in it's place on the page from my staging Rails site.

Also, locally on my OS X rails machine if I use "set names 'latin1'" before my queries, the characters all come back properly. I did have these tables set as latin1 before - could this be the issue?

Someone please help me out here, I'm going crazy trying to figure out what's wrong!

解决方案

AHA! Seems I had some table information encoded in latin1 before, and stupidly changed the databases to utf8 without converting.

Running the following fixed that currency_types table:

mysqldump -u root -p --opt --default-character-set=latin1 --skip-set-charset  DBNAME > DBNAME.sql

mysql -u root -p --default-character-set=utf8  DBNAME < DBNAME.sql

Now I just have to ensure that the other content generated after the latin1 > utf8 switch isn't messed up by that :(

这篇关于UTF8的Rails问题 - utf8_general_ci的编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 05:29
查看更多