Encoding a coding problem defined in the table provided inside MySQL

Sometimes we, but the Chinese did not insert a string of data into a table to insert the data table Chinese string is actually codec problem. Different time coding and definition table of coding MySQL internal settings, leading to encode abnormal, or is not able to insert data into the data table, or that the data has been inserted into the code table, but it will be garbled situation. Here is my personal solution that enables insertion of Chinese data:

View internal coding MySQL settings

The first is to view the internal coding MySQL settings. View contains global variables at the beginning of character

SHOW VARIABLES LIKE 'character%';

Results of the:

 

solution

Modify the internal coding MySQL settings

Modify Client , Connection , Results, Database, Server code to GBK or UTF8

  1. Modify the client encoding is UTF8

    SET character_set_client = utf8;
  2. Encoding modified connected to UTF8

    SET character_set_connection = utf8;
  3. Modify the query result is encoded as UTF8

    SET character_set_results = utf8;
  4. Modify the database encoding to UTF8

    SET character_set_database = utf8;
  5. Coded modify the database server is UTF8
    SET character_set_server = utf8;

View MySQL internal code again to see if the amendment is successful

SHOW VARIABLES LIKE 'character%';

 

Modified coding table provided

View table creation SQL statement

SHOW CREATE TABLE 表名;

As students watch:

SHOW CREATE TABLE students;

The table is already created for my test results are as follows:

CREATE TABLE `students` (
  `Id` int(30) NOT NULL,
  `Name` varchar(10) DEFAULT NULL,
  `Age` int(4) DEFAULT NULL,
  `Gender` varchar(25) DEFAULT NULL,
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

See CHARSET = latin1, i.e. encoding format students table is latin1.

 

Modified Table encoding format

The ALTER  TABLE table name CONVERT  the TO  CHARACTER  the SET new encoding format COLLATE collation;

As students view the above table:

ALTER TABLE students CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

The modified coding format table students utf8, MySQL the collation is utf8_general_ci

 

After successfully modified again to view SQL student table:

CREATE TABLE `students` (
  `Id` int(30) NOT NULL,
  `Name` varchar(10) DEFAULT NULL,
  `Age` int(4) DEFAULT NULL,
  `Gender` varchar(25) DEFAULT NULL,
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

Found that success has been modified. This time you can add the data to the Chinese students table.

 

Guess you like

Origin www.cnblogs.com/liyihua/p/12309804.html