1. Problems encounteredWhen storing Chinese characters in the database, I encountered this problem:
2. Analyze the problemThis is due to a problem with the database design. When the database was created, the default was selected directly without changing it to utf-8. Later, I tried to modify it manually but it still didn't work . I tried changing the default fields of the database and table to utf8, but Chinese characters still cannot be stored. 3. The real problem
4. SolutionExport our database as a sql file: /* SQLyog Enterprise v12.08 (64 bit) MySQL - 5.7.31 : Database - data_test ********************************************************************* */ /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; CREATE DATABASE /*!32312 IF NOT EXISTS*/`data_test` /*!40100 DEFAULT CHARACTER SET latin1*/; USE `data_test`; /*Table structure for table `user` */ DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `user_id` int(11) NOT NULL AUTO_INCREMENT, `user_name` varchar(50) CHARACTER SET latin1 NOT NULL COMMENT 'Account', `password` varchar(50) CHARACTER SET latin1 NOT NULL COMMENT 'password', `user_state` varchar(10) CHARACTER SET latin1 NOT NULL DEFAULT '1' COMMENT 'Status, logical deletion', PRIMARY KEY (`user_id`) ) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1; /*Data for the table `user` */ LOCK TABLES `user` WRITE; insert into `user`(`user_id`,`user_name`,`password`,`user_state`) values (1,'keafmd','keafmd','1'),(3,'21312321','123','0'),(11,'213','213','1'),(12,'keafmd','666','0'); UNLOCK TABLES; /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
The replaced sql file: /* SQLyog Enterprise v12.08 (64 bit) MySQL - 5.7.31 : Database - data_test ********************************************************************* */ /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; CREATE DATABASE /*!32312 IF NOT EXISTS*/`data_test` /*!40100 DEFAULT CHARACTER SET utf8 */; USE `data_test`; /*Table structure for table `user` */ DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `user_id` int(11) NOT NULL AUTO_INCREMENT, `user_name` varchar(50) CHARACTER SET utf8 NOT NULL COMMENT 'Account', `password` varchar(50) CHARACTER SET utf8 NOT NULL COMMENT 'password', `user_state` varchar(10) CHARACTER SET utf8 NOT NULL DEFAULT '1' COMMENT 'Status, logical deletion', PRIMARY KEY (`user_id`) ) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8; /*Data for the table `user` */ LOCK TABLES `user` WRITE; insert into `user`(`user_id`,`user_name`,`password`,`user_state`) values (1,'keafmd','keafmd','1'),(3,'21312321','123','0'),(11,'213','213','1'),(12,'keafmd','666','0'); UNLOCK TABLES; /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; Select Execute SQL script and select the modified SQL file Now check the table information
5. Solution EffectTest to verify whether the problem is really solved: This concludes this article about MySQL Basics - How to Solve Errors When Storing Chinese Characters into the Database. For more information on how to solve errors when storing Chinese characters into the database, please search for previous articles on 123WORDPRESS.COM or continue browsing the related articles below. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Teach you how to quickly enable self-monitoring of Apache SkyWalking
>>: Learn v-model and its modifiers in one article
Cerebro is an evolution of the Elasticsearch Kopf...
Table of contents Introduction Public code (backe...
In the previous article, I introduced the detaile...
Table of contents The use of Vue's keep-alive...
Background - Online Alert An online server issued...
This article shares the specific code of Vue usin...
Table of contents 1. Overview 2. Name field 3. Ve...
Table of contents 1. What is Bubble Sort 2. Give ...
The first method: Use Junge's one-click scrip...
Table of contents 1. let keyword 1.1 Basic Usage ...
float:left/right/none; 1. Same level floating (1)...
Table of contents Understanding Asynchrony fetch(...
Personally, I think the decompressed version is e...
This article example shares the specific code of ...
Overview I have been using Docker for more than a...