解决方法:
正如@Tim指出的那样,您需要将表/数据库/列的排序规则更改为UTF-8.首先检查数据库/表/列的排序规则.
检查分类:
如何检查数据库的排序规则:
SELECT
default_character_set_name
FROM
information_schema.SCHEMATA
WHERE
schema_name = "YOUR_DATABASE_NAME";
如何检查表的排序规则:
SELECT
CCSA.character_set_name
FROM
information_schema.`TABLES` T,
information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA
WHERE
CCSA.collation_name = T.table_collation
AND T.table_schema = "YOUR_DATABASE_NAME"
AND T.table_name = "YOUR_TABLE_NAME";
如何检查列的排序规则:
SELECT
character_set_name
FROM
information_schema.`COLUMNS`
WHERE
table_schema = "YOUR_DATABASE_NAME"
AND table_name = "YOUR_TABLE_NAME"
AND column_name = "YOUR_COLUMN_NAME";
更改收藏:
如何更改数据库排序规则:
ALTER DATABASE YOUR_DATABASE_NAME CHARACTER SET utf8 COLLATE utf8_unicode_ci;
如何更改表排序规则:
ALTER TABLE YOUR_TABLE_NAME CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
如何更改列排序规则:
ALTER TABLE YOUR_TABLE_NAME MODIFY YOUR_COLUMN_NAME VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci;
例:
DROP TABLE IF EXISTS `sample_table`;
CREATE TABLE `sample_table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`language` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO sample_table(name,language) VALUES('Ed Sheeran','English');
INSERT INTO sample_table(name,language) VALUES('আয়েশা খাতুন সুজানা','আমার সোনার বাংলা');
看,表定义中使用的CHARSET是utf8.因此,您可以在表中存储Unicode字符.
检查数据是否正确插入.
SELECT * FROM sample_table;
结果:
| id | name | language |
|----|--------------------|------------------|
| 1 | Ed Sheeran | English |
| 2 | আয়েশা খাতুন সুজানা | আমার সোনার বাংলা |