尝试存储表情符号时出错
Posted
技术标签:
【中文标题】尝试存储表情符号时出错【英文标题】:Error when try to store emoji 【发布时间】:2017-06-10 21:21:52 【问题描述】:我正在使用 sequelize 作为 node.js 的 ORM 与 mysql 数据库交互。
我的数据库字符集是utf8mb4, utf8mb4_general_ci
我的表格字符集是utf8mb4, utf8mb4_general_ci
我的列 TYPE 是 TEXT,字符集是 utf8mb4, utf8mb4_general_ci
在执行任何查询之前,我正在执行以下代码:
Sequelize.query("SET NAMES utf8mb4");
Sequelize.query("SET CHARACTER SET utf8mb4");
执行 Sequelize.query("SHOW VARIABLES LIKE 'character_set_%'") 后,我得到了:
[ [ RowDataPacket Variable_name: 'character_set_client', Value: 'utf8mb4' ,
RowDataPacket Variable_name: 'character_set_connection', Value: 'utf8mb4' ,
RowDataPacket Variable_name: 'character_set_database', Value: 'utf8mb4' ,
RowDataPacket Variable_name: 'character_set_filesystem', Value: 'binary' ,
RowDataPacket Variable_name: 'character_set_results', Value: 'utf8mb4' ,
RowDataPacket Variable_name: 'character_set_server', Value: 'utf8' ,
RowDataPacket Variable_name: 'character_set_system', Value: 'utf8' ,
RowDataPacket
Variable_name: 'character_sets_dir',
Value: 'c:\\openserver\\modules\\database\\MySQL-5.7-x64\\share\\charsets\\' ],
[ RowDataPacket Variable_name: 'character_set_client', Value: 'utf8mb4' ,
RowDataPacket Variable_name: 'character_set_connection', Value: 'utf8mb4' ,
RowDataPacket Variable_name: 'character_set_database', Value: 'utf8mb4' ,
RowDataPacket Variable_name: 'character_set_filesystem', Value: 'binary' ,
RowDataPacket Variable_name: 'character_set_results', Value: 'utf8mb4' ,
RowDataPacket Variable_name: 'character_set_server', Value: 'utf8' ,
RowDataPacket Variable_name: 'character_set_system', Value: 'utf8' ,
RowDataPacket
Variable_name: 'character_sets_dir',
Value: 'c:\\openserver\\modules\\database\\MySQL-5.7-x64\\share\\charsets\\' ] ]
但是当我试图保存包含表情符号的字符串时,我遇到了一个错误:
Unhandled rejection SequelizeDatabaseError: ER_TRUNCATED_WRONG_VALUE_FOR_FIELD: Incorrect string value: '\xF0\x9F\x91\x8D\xF0\x9F...' for column 'content' at row 1
【问题讨论】:
请提供SHOW CREATE TABLE
【参考方案1】:
我也遇到了这个问题,很容易解决,你只需要将你的数据库设置为utf8_general_ci。在 node.js 上进行连接时需要添加“charset”参数:
var pool = mysql.createPool(
host: "host",
user: "username",
password: "password",
database: "database",
connectionLimit: xxxx,
charset : 'utf8mb4'
);
这对我有用。
【讨论】:
utf8_general_ci
是 charater set utf8 的 collation,它 not 处理 Emoji。【参考方案2】:
我在尝试使用 Express 和 Sequelize 将表情符号插入 MySQL 时遇到了同样的问题。您可以确认您当前的字符集,如
SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';
上面的默认连接输出是这样的
character_set_client utf8
character_set_connection utf8
character_set_database utf8mb4
character_set_filesystem binary
character_set_results utf8
character_set_server utf8mb4
character_set_system utf8
collation_connection utf8_general_ci
collation_database utf8mb4_unicode_ci
collation_server utf8mb4_unicode_ci
如果你的 MySQL 配置文件 /etc/my.cnf 看起来像
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
但是如果你跑了
SET NAMES utf8mb4;
然后上面的 SHOW VARIABLES WHERE...
再次将全面更新为 utf8mb4。
character_set_client utf8mb4
character_set_connection utf8mb4
character_set_database utf8mb4
character_set_filesystem binary
character_set_results utf8mb4
character_set_server utf8mb4
character_set_system utf8
collation_connection utf8mb4_general_ci
collation_database utf8mb4_unicode_ci
collation_server utf8mb4_unicode_ci
所以,我找到的解决方案是将以下内容放在我的用户模型的第一行:
// make sure db/client/connection can support emoji
sequelize.query("SET NAMES utf8mb4;");
// confirm settings
sequelize.query("SHOW VARIABLES LIKE 'character_set_%'").then(function(data)
console.log(data);
);
【讨论】:
以上是关于尝试存储表情符号时出错的主要内容,如果未能解决你的问题,请参考以下文章
UITextField - 防止用户输入非标准字符(如表情符号)