MySQL reported an error when inserting Chinese into the table: ERROR 1366 (HY000)/ Incorrect string value/'\xE6\x9D\x8E\xE5\x8B\x87' for column'S

1. Error prompt

ERROR 1366 (HY000): Incorrect string value: ‘\xE6\x9D\x8E\xE5\x8B\x87’ for column ‘Sname’ at row 1

2. Process description

1. Create the following table
#建立一个学生表 
Create TABLE Student
	(Sno CHAR(9) PRIMARY KEY not null,/*列级完整性约束,Sno是主码*/
   Sname CHAR(20) not null,
   Ssex CHAR(2),
   Sage SMALLINT,
   Sdept CHAR(20)
  );
2. Insert the following tuple
#向学生表插入数据
insert 
into Student(Sno,Sname,Ssex,Sage,Sdept)
values('201215121','李勇','男',20,'CS');
3. The error is as follows
ERROR 1366 (HY000): Incorrect string value: '\xE6\x9D\x8E\xE5\x8B\x87' for column 'Sname' at row 1
  • Error interpretation: the string value in the "sname" column on line 1 \xE6\x9D\x8E\xE5\x8B\x87is incorrect
  • Reason for error: The reason is that the default character set of the established data table is latin1, and the character set of latin1 is 8bit, which cannot represent Chinese, so an error is reported.

Three, solve

1. View the table to build the structure
 show create table Student;
  • Shown as follows
    Insert picture description here

  • According to the description in the last line, the default table character set encoding is latin1

2. Modify the table character set encoding to utf8
alter table Student default character set utf8;
  • View the table again to build the structure

     show create table Student;
    
  • Shown as follows

    Insert picture description here

  • According to the description in the last line, the default table character set encoding has been changed to utf8

  • But when the tuple data is inserted into the table again, an error is still reported

    ERROR 1366 (HY000): Incorrect string value: '\xE6\x9D\x8E\xE5\x8B\x87' for column 'Sname' at row 1
    
  • The reason is that the above knowledge has modified the character set encoding of the table to utf8, but the character set encoding of the attribute column is still latin1 (as shown in the red rectangle in the figure above)

3. Modify the character set encoding of the attribute column to utf8
alter table Student change Sname Sname varchar(20) character set utf8;
  • View the table again to build the structure

  • Shown as follows

    Insert picture description here

  • According to the description in the last line, the character set encoding of the attribute column Sname has been changed to the default utf8

  • When inserting data into the table again, the error is changed to the attribute column Ssex, indicating that the character set encoding of the attribute column Sname just changed has passed

    ERROR 1366 (HY000): Incorrect string value: '\xE7\x94\xB7' for column 'Ssex' at row 1
    
  • Then modify the character set encoding of the attribute column Ssex to utf8

    alter table Student change Ssex Ssex varchar(20) character set utf8;
    
  • When you insert the tuple data into the table again, you can insert it normally

    Insert picture description here

  • If the character set encoding method of other attribute columns is still latin1, which causes an error to be reported, then continue to modify to utf8 as in the above method

4. If there are Chinese characters in the data table, when creating the table, you should specify the encoding method of the character set as utf8
#建立一个学生表 
Create TABLE Student
	(Sno CHAR(9) PRIMARY KEY not null,		/*列级完整性约束,Sno是主码*/
   Sname CHAR(20) not null,
   Ssex CHAR(2),
   Sage SMALLINT,
   Sdept CHAR(20)
  )ENGINE=InnoDB DEFAULT CHARSET=utf8;
  • When the encoding of the character set is specified as utf8 when building the table, the tuple data can be inserted normally in the subsequent

Guess you like

Origin blog.csdn.net/szw_yx/article/details/104695519