简体   繁体   English

我有一个坐在UTF-8 mysql数据库中的latin1编码数据,我该如何解决这个问题?

[英]I have latin1 encoded data sitting in a UTF-8 mysql database, how do I fix this?

I have latin1 encoded data sitting in a UTF-8 mysql database, how do I fix this? 我有一个坐在UTF-8 mysql数据库中的latin1编码数据,我该如何解决这个问题? There is no original data to go from unfortunately. 不幸的是,没有原始数据。

I figured out this much as the only way I could display the data correctly was to set everything latin1 in PHP, HTML and MySQL. 我认为这是我能正确显示数据的唯一方法是在PHP,HTML和MySQL中设置所有latin1。

Once this is completed, I can change everything back to utf-8 in my html and php. 一旦完成,我可以在我的html和php中将所有内容更改回utf-8。

Versions: mysql Ver 14.12 Distrib 5.0.51a, for debian-linux-gnu (x86_64) using readline 5.2 版本:mysql Ver 14.12 Distrib 5.0.51a,debian-linux-gnu(x86_64)使用readline 5.2

EDIT: I should mention, everything is working OK as I am telling PHP and HTML to use latin1 encoding, however, this just seems bad to me. 编辑:我应该提一下,一切正常,因为我告诉PHP和HTML使用latin1编码,但是,这对我来说似乎不好。

I believe that this article does exactly what you need to . 我相信这篇文章完全符合您的需求

I've paraphrased the steps you need to take below - replace 'MyDb' with the name of your database. 我已经解释了你需要采取的步骤 - 用你的数据库名称替换'MyDb'。 I would recommend making a backup before you begin! 我建议您在开始之前进行备份!

USE information_schema;
SELECT CONCAT('ALTER TABLE ', table_name, ' MODIFY ', column_name, ' ', REPLACE(column_type, 'char', 'binary'), ';') FROM columns WHERE table_schema = 'MyDb' and data_type LIKE '%char%';
SELECT CONCAT('ALTER TABLE ', table_name, ' MODIFY ', column_name, ' ', REPLACE(column_type, 'text', 'blob'), ';') FROM columns WHERE table_schema = 'MyDb' and data_type LIKE '%text%';
SELECT CONCAT('ALTER TABLE ', table_name, ' MODIFY ', column_name, ' ', column_type, ' CHARACTER SET utf8;') FROM columns WHERE table_schema = 'MyDb' and data_type LIKE '%char%';
SELECT CONCAT('ALTER TABLE ', table_name, ' MODIFY ', column_name, ' ', column_type, ' CHARACTER SET utf8;') FROM columns WHERE table_schema = 'MyDb' and data_type LIKE '%text%';

Copy the output of all the SELECT statements above into a SQL script. 将上面所有SELECT语句的输出复制到SQL脚本中。 Add the following to it: 添加以下内容:

ALTER DATABASE MyDb CHARACTER SET utf8;

Switch to MyDb ( USE MyDb; ) and run the SQL script. 切换到MyDb( USE MyDb; )并运行SQL脚本。

You could try run the following query: 您可以尝试运行以下查询:

ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

NOTE: I haven't tried this, but this is taken from the MY-SQL docs ( http://dev.mysql.com/doc/refman/5.0/en/charset-column.html ). 注意:我没有尝试过这个,但这取自MY-SQL文档( http://dev.mysql.com/doc/refman/5.0/en/charset-column.html )。
The page linked above has some more information, but this seems to be the query that you want (it is posted right at the bottom in case you were wondering). 上面链接的页面有一些更多的信息,但这似乎是你想要的查询(它是在你想知道的情况下发布在底部)。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM