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-- character Encoding and character set

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Foreword:

Generally speaking, the occurrence of Chinese garbled codes is caused by the mismatch between the client and server character sets. (database tables created with no specified character set by default are all latinl character sets. It is strongly recommended to use utf8 character sets.)

The idea of ensuring that there is no garbled code: ensuring the unity of client, server, database and table character set

Pit: sometimes, there is a problem with the character set of cmd or linux system. In fact, the database is correct, but when we execute the command, we see garbled code on the terminal. In this case, we need to set the character set of linux system

1. View the liunx character set: cat / etc/sysconfig/i18n

2 、 vim / etc/sysconfig/i18n

Note: always keep in mind that the data is stored in the field, so the coding is finally based on the field code, and the default value of the field is affected when the database and table coding is affected, that is to say, after the database code is set, the newly created table will be the same as the database code without specifying the field code, and the field code will be the same as the table when the field code is not specified. If you use Alter to modify the character encoding of a table, the original field encoding will not change. If you need to modify the table encoding as well as the field encoding, you can use convert

1. Check what character set is 1. Find out what character set the database is. Show create database django\ G (django is the name of the database)

2. What character set is the lookup table show create table auth_group\ G (auth_group is the name of the table)

ENGINE = InnoDB the table database engine is InnoDBDEFULT CHARSET=utf8 the table character set is utf8COLLATE utf8_general_ci calibration rule 3, view all character sets and corresponding calibration rules show character set

2. Solution: 1. When inserting data, first set the client to insert the same character set as the database table eg: if the database table character set is latinl, then insert the data using the latinl method: set names latinlinsert into student value ('Feidong')

2. Put the SQL statement into the file, use source to execute the file, insert the data, there will be no garbled, the principle is the same as 1, just put it in the file to execute

Method: put the following two statements into a new file, such as mysql.sql

Set names Latinlinsert into student value ('Feidong') executes this file: source mysql.sql

3. Import the database from other files (avoid garbled Chinese characters in the imported data)

Save the data encoding format as utf-8 (UTF8 do not import gbk,gbk do not import UTF8)

Set names utf8;ALTERDATABASEDEFAULTCHARACTERSETCOLLATE utf8_general_ci; modifies the encoding format of the database ALTERTABLEDEFAULTCHARACTERSETCOLLATE utf8_general_ci; modifies the encoding format of the table LOADLOCAL'C:\ utf8.txt'INTOTABLE tb_name; imports the database from the file 4, permanently modifies the default client encoding format (modifies the my.cnf configuration file for MySQL version 5.5 and above)

5. Permanently modify the default server encoding format (modify the my.cnf configuration file for MySQL version 5.5 or above)

Restart the mysqld service after modification: service mysqld restart

3. Common commands 1. View the default encoding format show variables like "% char%"

The character sets of character_set_client (client), character_set_connection (connection) and character_set_results (result) are consistent. 2. Modify the encoding format of the database ALTERDATABASEDEFAULTCHARACTERSETCOLLATE utf8_general_ci; 3, modify the encoding format of the table ALTERTABLEDEFAULTCHARACTERSETCOLLATE utf8_general_ci 4, modify the character set of the library or table through ALTER, only valid for the data inserted later, and the existing data cannot be modified. If you need to modify the original character set with data, you can only export the original data and insert it into the new table (database) that has modified the character set.

Recommendation 1. In the Chinese and English environment, linux sets the character set of the system to utf8, database server, client, database and table are all utf8 character set 2. No matter searching or inserting, first set the character set with set names utf8. 3. It is recommended to build a database statement:

4. Suggestion table statement:

4. Suggestion table statement:

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

Database

Wechat

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

12
Report