使用 UTF8 格式化 MySQL 命令行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6787824/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:35:01  来源:igfitidea点击:

MySQL command line formatting with UTF8

mysqlutf-8

提问by spier

I have a database table that contains Swedish/Norwegian strings.

我有一个包含瑞典语/挪威语字符串的数据库表。

When I query some data, I get output like this:

当我查询一些数据时,我得到如下输出:

Output with set names latin1;

输出与 set names latin1;

+-----------------------------------+
| name                              |
+-----------------------------------+
| Kid Interi#####                   | 
| Bwg Homes                         | 
| If Skadef####kring                | 
| Jangaard Export                   | 
| Nordisk Film                      | 
+-----------------------------------+

Now if I set names utf8;in order to see the characters with their proper encoding, then the formatting of the tabular output of the MySQL command line breaks.

现在,如果我set names utf8;为了查看具有正确编码的字符,则 MySQL 命令行的表格输出的格式会中断。

Output with set names utf8;

输出与 set names utf8;

+-----------------------------------+
| name                              |
+-----------------------------------+
| Kid Interi?r                     | 
| Bwg Homes                         | 
| If Skadef?rs?kring              | 
| Jangaard Export                   | 
| Nordisk Film                      | 
+-----------------------------------+

Question:

题:

This is not a big issue but it makes the output a bit harder to read. Does anybody know how to keep the tabular formatting intact?

这不是一个大问题,但它使输出有点难以阅读。有人知道如何保持表格格式不变吗?

回答by Martin Taleski

Short answer

简答

Start the client with option --default-character-set=utf8:

使用选项启动客户端--default-character-set=utf8

mysql --default-character-set=utf8

You can set this as a default in the /etc/mysql/my.cnffile.

您可以将其设置为/etc/mysql/my.cnf文件中的默认值。

[mysql]
default-character-set=utf8

The short answer did not work, read bellow

简短的回答不起作用,请阅读以下内容

The command above forces the character_set_client, character_set_connectionand character_set_resultsconfig variables to be utf8.

上面的命令强制character_set_client,character_set_connectioncharacter_set_resultsconfig 变量为utf8.

In order to check the values for all the charset related config variables you can run:

为了检查所有与字符集相关的配置变量的值,您可以运行:

show variables like '%char%';

The character_set_databasegives you the character set of the current database (schema) that you are in. The schema and tables are created by default with the charset specified in the character_set_server, unless it is specified explicitly in the CREATEstatement.

character_set_database给你的字符集当前数据库(模式)的,你是在该模式和表默认创建在指定的字符集character_set_server,除非是在明确指定CREATE的语句。

The character_set_servercan be changed in the my.cnffile:

character_set_server可以在中更改my.cnf文件:

[mysqld]
character-set-server = utf8

Additionally tables and columns can have their own charset which might be different from their parent table or schema. To specifically check the values of each table and column in a database see this answer: How do I see what character set a MySQL database / table / column is?

此外,表和列可以有自己的字符集,这可能与其父表或架构不同。要专门检查数据库中每个表和列的值,请参阅此答案: 如何查看 MySQL 数据库/表/列是什么字符集?

If you want to change the character set of existing tables and columns, see this answer: How to convert an entire MySQL database characterset and collation to UTF-8?

如果要更改现有表和列的字符集,请参阅此答案:如何将整个 MySQL 数据库字符集和排序规则转换为 UTF-8?

More info on connection character sets in the mysql docsumentation.

mysql 文档中有关连接字符集的更多信息。

Everything is set to utf8, but I still see weird characters

一切都设置为utf8,但我仍然看到奇怪的字符

Even if all the charsets variables, tables and columns are set to utf8, there might be cases where you see weird characters on your screen. For example, somebody might have written Unicode characters in a utf8column, through a client with latin1connection (for example by running mysql --default-character-set=latin1). In this case you need to connect to the database with the same charset as the values were written. You can also retrieve and rewrite them through the correct encoding.

即使所有字符集变量、表和列都设置为utf8,您也可能会在屏幕上看到奇怪的字符。例如,某人可能utf8通过具有latin1连接的客户端(例如通过运行mysql --default-character-set=latin1)在列中写入了 Unicode 字符。在这种情况下,您需要使用与写入值相同的字符集连接到数据库。您还可以通过正确的编码检索和重写它们。

NOTE: As the comments point out, the myslq utf8encoding is not a true and full implementation of UTF-8. If a full implementation of UTF-8 is needed, one can use the utf8mb4charset:

注意:正如评论所指出的,myslqutf8编码不是 UTF-8 的真实和完整实现。如果需要完整的 UTF-8 实现,可以使用utf8mb4字符集:

mysql --default-character-set=utf8mb4

More info here: What is the difference between utf8mb4 and utf8 charsets in MySQL?

更多信息:MySQL 中的 utf8mb4 和 utf8 字符集有什么区别?

回答by zuanfan

These words "? ? ?" with utf8 takes 2 bytes, so did you forget use wchar or utf string?

这些字 ”? ? ?” utf8 需要 2 个字节,所以你忘记使用 wchar 或 utf 字符串了吗?

Here's my test code in python:

这是我在python中的测试代码:

s = ["Kid Interi?r","Bwg Homes","If Skadef?rs?kring"]
for w in s:
    print '|',w.ljust(20,' '),'|' 

the result is as the same as your program print out. all I need to do is change the encoding of string s:

结果和你的程序打印出来的一样。我需要做的就是更改字符串的编码s

s = [u"Kid Interi?r",u"Bwg Homes",u"If Skadef?rs?kring"]
for w in s:
    print '|',w.ljust(20,' '),'|'

the result is

结果是

| Kid Interi?r         |
| Bwg Homes            |
| If Skadef?rs?kring   |

I haven't test in c++, but I suggest you can use wchar, std::wcout.

我没有用 c++ 测试过,但我建议你可以使用 wchar、std::wcout。