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

The solution to the problem of Chinese garbled Code in inserting Table data in mysql

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

Share

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

First, problems

When you insert other database data into a mysql database table during development, you will always report an error similar to the following:

Incorrect string value:'\ xE6\ x88\ x91 'for column' name' at row 1

2. Analysis

1. I first checked that the corresponding type of this field in both database tables is varchar, so there is no problem of type inconsistency leading to insertion errors.

two。 After excluding the error caused by the inconsistent field type, I guess it is the Chinese garbled problem, and then insert the pure English data and find that there is no error, so it is determined that it is the Chinese garbled problem. Then Baidu found that the Chinese garbled code was caused by the default coding problem of mysql.

III. Solutions

1. Check the my.ini configuration file under the local mysql installation file directory, and whether the default encoding of the server and client is utf8.

[mysqld] # the character set used by the server defaults to UTF8character-set-server=utf8 [client] # sets the default port default-character-set=utf8 for mysql clients to connect to the server

two。 After the previous step is correct, open the command line, type: net start mysql, and start the mysql service

After displaying "Service has started successfully", enter the command "mysql" to use the database

View the character set encoding format of the database table: enter the command "show create table test.xtt_test_copy;"

As shown in the figure above, it is found that the default is "latin1" and not "utf8". You need to continue to modify it manually. Enter the command "ALTER TABLE test.xtt_test_copy CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;" and continue to enter the command "show create table test.xtt_test_copy;" to see that the character set encoding of the default database table has been changed to "utf8".

View the character set encoding format of the database: enter the command "show creat database test;"

As shown in the figure above, it is found that the default is "latin1" rather than "utf8". You need to continue to modify it manually. Enter the command "ALTER DATABASE test DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;", continue to enter the command "SHOW CREATE database test;", and check that the character set encoding of the default database table has been changed to "utf8".

3. Finally, just restart the service.

Mysql > net stop mysql;mysql > net start mysql

In the process of using mysql, we often encounter Chinese garbled codes. According to my usual work experience, I have summed up the following four points that need to be paid attention to:

1. Mysql startup character set. Configure the default-character-set= character set in the myini file and put it on [WinMySQLadmin].

two。 Set the character set in the process of creating the table in the database, and set it in the CHARSET= character set statement at the end of the CREATE statement

3. Set the character set, jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding= character set in the options in the driver url

4. Set the displayed character set in the web display page and response.setContentType ("text/html;charset= character set") in servelt

Summary

The above is the whole content of this article, I hope that the content of this article has a certain reference and learning value for your study or work, if you have any questions, you can leave a message and exchange, thank you for your support.

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