mysql failed to insert Chinese characters, modify the field character set to utf-8, and insert Chinese characters normally

Mysql failed to insert Chinese characters

ERROR 1366 (22007): Incorrect string value: \
'\xE6\x9D\x8E' for column `poetry_db`.`author`.`name` at row 1

Change the charset of the database

Use alter database dbname default character set 'utf8';the charset to modify the database

$ show create database poetry_db;
+-----------+----------------------------------------------------------------------+
| Database  | Create Database                                                      |
+-----------+----------------------------------------------------------------------+
| poetry_db | CREATE DATABASE `poetry_db` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+-----------+----------------------------------------------------------------------+
$ alter database poetry_db default character set 'utf8';
$ show variables like 'character%';
+--------------------------+----------------------------+
| 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       | /usr/share/mysql/charsets/ |

found the following

| character_set_database   | utf8                       |

Or insert an error

Change the charset of the table

Use alter table tbname default character set 'utf8';the charset to modify the table

$ show create table author;
+--------+----+
| Table  | Create Table|
+--------+-----+
| author | CREATE TABLE `author` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(16) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1 |

$ alter table author default character set 'utf8'; 

$ show create table author;
+--------+----+
| Table  | Create Table|
+--------+-----+
| author | CREATE TABLE `author` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(16) CHARACTER SET latin1 DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8 |

Or insert an error

Change the charset of a table field

Use ```alter table tbname change fieldname fieldname xxx character set utf8 xxx;`` modify field`

$ alter table author change name name varchar(16) character set utf8 default null;
$ insert into author(name) values("李");
Query OK, 1 row affected (0.063 sec)

Finally inserting Chinese characters is normal

Guess you like

Origin blog.csdn.net/coraline1991/article/details/120324320