Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

MySQL can't show how to solve the Chinese problem.

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

The knowledge of this article "MySQL can not show how to solve Chinese problems" is not quite understood by most people, so the editor summarizes the following contents, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "MySQL can not show how to solve Chinese problems" article.

MySQL does not display Chinese

problem

Invalid method 1SHOW VARIABLES LIKE'% character%'

SET character_set_client = utf8SET character_set_server = utf8 effective method 2

Specify the type when you create the table

MySQL character set problem

MySQL character set support (Character Set Support) has two aspects:

Character set (Character set) and sorting method (Collation).

Support for character sets is refined to four levels:

Server (server)

Database (database)

Data sheet (table)

Connect (connection)

1.MySQL default character set

MySQL's specification of a character set can be refined into a database, a table, a column, and what character set should be used.

However, traditional programs do not use such complex configurations when creating databases and data tables, they use the default configuration, so where does the default configuration come from?

(1) when compiling MySQL, you specify a default character set, which is latin1

(2) when installing MySQL, you can specify a default character set in the configuration file (my.ini). If not, this value inherits from the

(3) when you start mysqld, you can specify a default character set in the command line parameters. If not, this value inherits from the configuration in the configuration file. In this case, character_set_server is set to the default character set.

(4) when creating a new database, unless explicitly specified, the character set of the database is set to character_set_server by default

(5) when a database is selected, character_set_database is set as the default character set for that database.

(6) when creating a table in this database, the default character set of the table is set to character_set_database, which is the default character set of the database.

(7) when setting a column in a table, unless explicitly specified, the default character set of this column is the default character set of the table.

A brief summary:

If nothing is changed, then all tables in all databases and all fields are stored in latin1, but if we install MySQL, we usually choose multi-language support, that is, the installer will automatically set default_character_set to UTF-8 in the configuration file, which ensures that by default, all tables in all databases are stored in UTF-8.

two。 View the default character set (by default, the character set of mysql is latin1 (ISO_8859_1)

In general, you can view the character set and sorting method of the system through the following two commands:

Mysql > SHOW VARIABLES LIKE 'character%' +-- +-- + | Variable_name | Value | +-- -+ | character_set_client | latin1 | | character_set_connection | latin1 | | character_set_database | latin1 | | character_set_filesystem | binary | | character_set_results | latin1 | | character_set_server | latin1 | character_set_system | utf8 | | character_sets_dir | D: "mysql-5.0.37" share "charsets" | +-- -- + mysql > SHOW VARIABLES LIKE 'collation_%' +-- +-+ | Variable_name | Value | +-+-+ | collation_connection | utf8_general_ci | | collation_database | utf8_general_ci | | collation_server | utf8_general_ci | +- -+ 3. Modify the default character set

(1) the easiest way to modify is to modify the key values of the character set in the my.ini file of mysql.

Such as:

Default-character-set = utf8character_set_server = utf8

After modification, restart the mysql service, service mysql restart

Use

Mysql > SHOW VARIABLES LIKE 'character%'

Check and find that the database codes have been changed to utf8

+-- +-- + | Variable_name | Value | +-- -+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | character_set_system | utf8 | | character_sets_dir | D: "mysql-5.0.37" share "charsets" | +-- -- +

(2) there is another way to modify the character set, which is to use the command of mysql

Mysql > SET character_set_client = utf8; mysql > SET character_set_connection = utf8; mysql > SET character_set_database = utf8; mysql > SET character_set_results = utf8; mysql > SET character_set_server = utf8; mysql > SET collation_connection = utf8; mysql > SET collation_database = utf8; mysql > SET collation_server = utf8

In general, even if you set the default character set of the table to utf8 and send the query through UTF-8 encoding, you will find that the code stored in the database is still garbled. The problem lies in this connection connection layer. The solution is to execute the following sentence before sending the query:

SET NAMES 'utf8'

It is equivalent to the following three instructions:

SET character_set_client = utf8;SET character_set_results = utf8;SET character_set_connection = utf8

Summary:

Therefore, it doesn't matter to us what database version we use, whether it's 3.x, 4.0.x or 4.1.x, there are two important things:

1) set the database code correctly. The character set of MySQL version 4.0 and below always defaults to ISO8859-1 MySQL 4.1, which you can choose when installing. If you are going to use UTF-8, you should specify UTF-8 when you create the database (you can also change it after you have created it. Version 4.1 or above can also specify the character set of the table separately)

2) set the database connection code correctly. After setting up the encoding of the database, you should specify the encoding of connection when connecting to the database, for example, when using jdbc connection, specify the connection as utf8 mode.

The above is about the content of this article "MySQL can not show how to solve Chinese problems". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report