MySql varchar 从 Latin1 更改为 UTF8

发布于 2024-09-07 01:10:48 字数 169 浏览 10 评论 0原文

在 mySql 表中,我使用 Latin1 字符集将文本存储在 varchar 字段中。由于我们的网站现在在更多国家/地区得到支持,因此我们需要支持 UTF8。如果我将这些字段更改为 UTF8 会发生什么情况?这样做是否安全,或者会弄乱这些字段内的数据吗?将字段更改为 UTF8 时我需要考虑什么吗?

谢谢!

In a mySql table I'm using Latin1 character set to store text in a varchar field. As our website now is supported in more countries we need support for UTF8 instead. What will happen if I change these fields to UTF8 instead? Is it secure to do this or will it mess up the data inside these fields? Is it something I need to think about when changing the field to UTF8?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烙印 2024-09-14 01:10:51

MySQL 很好地处理了这个问题:

CREATE TEMPORARY TABLE t1 (
  c VARCHAR(10)
) CHARACTER SET ="latin1";

INSERT INTO t1 VALUES ("æøå");
SELECT * FROM t1; # 'æøå'

ALTER TABLE t1 CHARACTER SET = "utf8";
SELECT * FROM t1; # 'æøå'

DROP TEMPORARY TABLE t1;

编辑:并且没有不能存储为 utf-8 的 latin-1 字符,因此您不应该丢失任何数据

MySQL handles this nicely:

CREATE TEMPORARY TABLE t1 (
  c VARCHAR(10)
) CHARACTER SET ="latin1";

INSERT INTO t1 VALUES ("æøå");
SELECT * FROM t1; # 'æøå'

ALTER TABLE t1 CHARACTER SET = "utf8";
SELECT * FROM t1; # 'æøå'

DROP TEMPORARY TABLE t1;

EDIT: And there are no latin-1 characters that cannot be stored as utf-8, so you shouldn't get any dataloss

£烟消云散 2024-09-14 01:10:51

数据库方面应该不会有任何问题。 MySQL 将在您进行更改时处理编码之间的数据转换。

当您向网站添加对 UTF-8 的支持时,还需要注意其他事项。

  • 为了获得最佳结果,您应该确保与数据库的连接使用 UTF-8 作为连接字符集。您可以通过在建立连接时发出 SET NAMES utf8 来完成此操作。
  • 您需要通过更改元标记来确保您的网页本身以 UTF-8 编码向浏览器声明。
  • 安全注意事项:您应该检查用户输入以确保其是有效的 UTF-8,以避免跨站点脚本 (XSS) 攻击的可能性

There shouldn't be any problems on the database front. MySQL will handle the conversion of data between encodings at the time you make the change.

There are other things you need to be aware of when you add support for UTF-8 to your website.

  • For best results, you should make sure that connections to the database are using UTF-8 as the connection character set. You can do this by issuing SET NAMES utf8 when you make the connection.
  • You need to make sure that your web pages themselves are declared to the browser as encoded in UTF-8, by changing the meta tag.
  • Security considerations: You should check user input to make sure it is valid UTF-8, to avoid the possibility of cross-site scripting (XSS) attacks
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文