ERROR 1366 (HY000): Incorrect string value, mysql error when inserting data? arrange

1. Report an error

        Mysql57, windows environment, error when inserting Chinese data

ERROR 1366 (HY000): Incorrect string value: '\xC0\xEE\xCB\xC4' for column 'nm' at row 1

2. Test statement

        ​​Create a database, create a table, insert Chinese data and report an error 1366

--创建数据库
CREATE DATABASE anadb
--如果MySQL中不存在相关的数据库,则创建数据库;如果MySQL中已经存在相关的数据库,则忽略创建语句,不再创建数据库。
--在实际工作中,建议使用CREATE DATABASE IF NOT EXISTS database_name语句创建数据库。
CREATE DATABASE IF NOT EXISTS anadb。
--指定字符集创建 ENCRYPTION='N'不使用MySQL的加密技术
CREATE DATABASE [IF NOT EXISTS] database_name DEFAULT CHARACTER SET character_name COLLATE collate_name 
  [DEFAULT ENCRYPTION='N']; 
--查看MySQL命令行所在的数据库
SELECT DATABASE();
--切换
USE anadb;
--查看数据库的创建信息
SHOW CREATE DATABASE anadb;
SHOW CREATE DATABASE anadb \G
*************************** 1. row ***************************
       Database: anadb
Create Database: CREATE DATABASE `anadb` /*!40100 DEFAULT CHARACTER SET latin1 */
1 row in set (0.00 sec)
--建表
CREATE TABLE IF NOT EXISTS table_test(id int,nm varchar(10),dt date);   
SHOW TABLES;
--删除数据库
DROP DATABASE anadb;
--在实际工作中,建议使用DROP DATABASE IF EXISTS database_name语句删除数据库  
DROP DATABASE IF EXISTS anadb 

--插入数据
insert into table_test values(1,'zhangsan','2023-10-16');
insert into table_test values(2,'李四','2023-10-16');

3. Query the reason

mysql> show variables like '%char%';
+--------------------------+----------------------------------+
| Variable_name            | Value                            |
+--------------------------+----------------------------------+
| character_set_client     | utf8    |客户端使用的字符集
| character_set_connection | utf8    |连接数据库的字符集设置类型,如果成行没有指明连接数据库使用的字符集类型则按照服务器端默认的字符集设置。
| character_set_database   | latin1  |数据库服务器中表和字段默认使用的字符集设定,如果表和字段没有设定字符集,默认使用此字符集。
| character_set_filesystem | binary                           |
| character_set_results    | utf8    |数据库给客户端返回时使用的字符集设定,如果没有指明,使用服务器默认的字符集。
| character_set_server     | latin1  |为服务器安装时指定的默认字符集设定。 创建数据库时的默认字符集
| character_set_system     | utf8                             |
| character_sets_dir       | D:\vmdata\mysqlw\share\charsets\

        Query the character set of mysql. Now the database and server are Latin1. We did not specify the character set when creating the database, so the database and table are both latin1.

--查看数据库设置的字符集
show create database anadb;
--查看表中的字段设置的字符集:
show full columns from table_test1;
--查看表设置的字符集
show create table table_test1\G;

 --Create a database. If the default character set is not specified, the system will set it according to the value of character_set_server
CREATE DATABASE db_name DEFAULT CHARACTER SET utf8;
--Create a table. If you do not specify the default character set, the system will set it according to the value of the database
CREATE TABLE db_name.tb_name (id VARCHAR(20) NOT NULL,name VARCHAR (20) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  

           Pay special attention to the character set of the column here. The character set of this column is latin1, which is the main problem causing the insertion failure.

4.Solution 

        I found the reason and solved it permanently.

设置 my.ini中的字符集

[mysqld]
character-set-server = utf8
这个参数 控制character-set-server和character_set_database的值;控制数据库,表,字段默认的字符集

set names utf8
控制当前session的character_set_connection、character_set_client、character_set_results的字符集;控制客户端查看数据库时的乱码问题

        ​ ​ Here add a line character-set-server = utf8 under mysqld in the my.ini file 

        Restart the database and check the character set

        In this way, no errors will be reported when inserting data when building databases and tables.

5. alter modifies the character set

        After we changed the character set, the original anadb library was deleted and rebuilt or modified directly with alter (changing with alter is not as fast as rebuilding)

--修改数据库字符集
alter database anadb character set utf8;
--修改表的字符集
ALTER TABLE table_test DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE table_test DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
--修改列的字符集
ALTER TABLE table_test CHANGE nm nm VARCHAR(10) CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE table_test CHANGE nm nm VARCHAR(10) CHARACTER SET gbk COLLATE gbk_chinese_ci;

        ​ ​ ​ Pay special attention to the character set of the column. The character set of this column is latin1, which is the main problem that causes insertion failure;

       Modify the character set of the column; or after changing the database encoding, delete the table directly and rebuild it. This will be much faster and there is no need to change the fields separately.

 

Guess you like

Origin blog.csdn.net/qq_63693805/article/details/133863564