如何在 MySQL 中存储 unicode?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3328968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to store unicode in MySQL?
提问by TCM
How do I store Unicode in free edition of MySQL?
如何在 MySQL 免费版中存储 Unicode?
There doesn't seem to be nvarchar
type as in SQL Server. Is Unicode not supported in MySQL? I tried using text
but that too is not working.
似乎没有nvarchar
SQL Server 中的类型。MySQL 不支持 Unicode 吗?我尝试使用,text
但这也不起作用。
采纳答案by Pekka
You need to choose a utf8_*
character set for your table. Text and memo fields will then automatically be stored in UTF-8. Support for UTF-16 is coming in mySQL 6.
您需要utf8_*
为您的表格选择一个 字符集。文本和备注字段将自动以 UTF-8 格式存储。mySQL 6 中将支持 UTF-16。
回答by Stefan Gehrig
The character set for a given string column (CHAR
, VARCHAR
or *TEXT
) is determined by its character setand/or collation. This is a quite intense topic so it's best to read the documentation I linked. For example:
给定字符串列(CHAR
,VARCHAR
或*TEXT
)的字符集由其字符集和/或排序规则确定。这是一个非常激烈的话题,所以最好阅读我链接的文档。例如:
CREATE TABLE t1
(
col1 CHAR(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci
)
will create a table t1
with a col1
that stores its content in UTF-8 encoding.
将创建一个表t1
具有col1
存储其在UTF-8编码的内容。
回答by chandrageetha
I follow my own style of coding, so please take that into account when following my lead.
我遵循我自己的编码风格,所以在遵循我的指导时请考虑到这一点。
First I created the MySql database.
首先,我创建了 MySql 数据库。
after creating database, in mysql command prompt, give the command:
创建数据库后,在mysql命令提示符下,给出命令:
SET NAMES = UTF8;
table and columns to store unicode are to be set with collation property as utf8-utf_general_ci. Each and every column which meant to store unicode, is to be selected and set collation property to utf8-utf_general_ci.
用于存储 unicode 的表和列将使用整理属性设置为 utf8-utf_general_ci。将选择用于存储 unicode 的每一列并将排序规则属性设置为 utf8-utf_general_ci。
Now in C#.
现在在 C# 中。
my connection string is set as usual but with addition of a single attribute like this:
我的连接字符串像往常一样设置,但添加了一个这样的属性:
constring = @"SERVER=localhost;" + @"PORT=3306;" + @"DATABASE=gayakidb;" + @"UID=root;" + @"PASSWORD=mysql;" + @"charset=utf8;";
Next, installed unicode font in the 'Display language setting' of your system OS. Or, you can copy and paste the font file (true type file) in the Fonts folder of Operating System.
接下来,在系统操作系统的“显示语言设置”中安装 unicode 字体。或者,您可以将字体文件(真字体文件)复制并粘贴到操作系统的 Fonts 文件夹中。
Then I set the font property of the textbox object or any other tool object in property page.
然后我在属性页中设置文本框对象或任何其他工具对象的字体属性。
More over, if you need to type in unicode font using your keyboard, you need to install the unicode language for keyboard layout. That could be done using the option for regional language settings of your Operating System.
此外,如果您需要使用键盘输入 unicode 字体,则需要为键盘布局安装 unicode 语言。这可以使用操作系统的区域语言设置选项来完成。
Done. with these settings, i coded module for saving data and while running the ocde, it successfully done the work.
完毕。通过这些设置,我编写了用于保存数据的模块,并且在运行 ocde 时,它成功地完成了工作。
回答by Eimantas
Have you tried setting names after connection? What was the outcome of tryng to store unicode characters? Connect to mysql server and type this:
您是否尝试过在连接后设置名称?尝试存储 unicode 字符的结果是什么?连接到mysql服务器并输入:
SET NAMES UTF8;
This should turn on "support" for utf8. Then try storing utf data.
这应该打开对 utf8 的“支持”。然后尝试存储 utf 数据。
回答by Martin Vseticka
- MySQL supportsUTF-8.
- Use varchar and set encoding for the column (it's quite easy in phpMyAdmin)
- MySQL支持UTF-8。
- 使用 varchar 并为列设置编码(在 phpMyAdmin 中很容易)
http://www.oreillynet.com/onlamp/blog/2006/01/turning_mysql_data_in_latin1_t.html- I think this could help you
http://www.oreillynet.com/onlamp/blog/2006/01/turning_mysql_data_in_latin1_t.html- 我认为这可以帮助你
回答by Martin Vseticka
Use this Query
使用此查询
alter table `DBNAME`.`TblName` Engine=InnoDB checksum=1 comment='' delay_key_write=1 row_format=dynamic charset=utf8 collate=utf8_unicode_ci