如何在 MySQL 数据库中存储 Emoji 字符
2021-11-20 00:00:00
mysql
我在我的项目中使用了表情符号.这些字符被保存(??)到 mysql 数据库中.我在 utf8mb4_general_ci
中使用了数据库默认排序规则.它显示
I am using Emoji character in my project. That characters are saved (??) into mysql database. I had used database Default collation in utf8mb4_general_ci
.
It show
1366 不正确的字符串值:'\xF0\x9F\x98\x83\xF0\x9F...' 列第 1 行的评论"
1366 Incorrect string value: '\xF0\x9F\x98\x83\xF0\x9F...' for column 'comment' at row 1
推荐答案
第 1 步,更改数据库的默认字符集:
Step 1, change your database's default charset:
ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
如果尚未创建数据库,请使用正确的编码创建它:
if the db is not created yet, create it with correct encodings:
CREATE DATABASE database_name DEFAULT CHARSET = utf8mb4 DEFAULT COLLATE = utf8mb4_unicode_ci;
第2步,创建表时设置字符集:
Step 2, set charset when creating table:
CREATE TABLE IF NOT EXISTS table_name (
...
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;
或修改表格
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE table_name MODIFY field_name TEXT CHARSET utf8mb4;
相关文章