【开发踩坑】生僻字插入MySQL失败

背景

生产环境插入数据报错:

bash 复制代码
java.sql.SQLException: Incorrect string value: '\xF0\xAC\xB1\x96' for column 'answer' at row 1

设置answer字段值为 "𬱖"出现错误

生僻字设置出错;

排查

编码

查看库表属性:

SQL 复制代码
ENGINE InnoDB AUTO_INCREMENT =1479001 DEFAULT CHARSET utf8 ROW_FORMAT DYNAMIO
utf8

10.1.10.6 The utf8mb4 Character Set (4-Byte UTF-8 Unicode Encoding)

The character set named utf8 uses a maximum of three bytes per character and contains only BMP characters. As of MySQL 5.5.3, the utf8mb4 character set uses a maximum of four bytes per character supports supplemental characters:

For a BMP character, utf8 and utf8mb4 have identical storage characteristics: same code values, same encoding, same length.

For a supplementary character, utf8 cannot store the character at all, while utf8mb4 requires four bytes to store it. Since utf8 cannot store the character at all, you do not have any supplementary characters in utf8 columns and you need not worry about converting characters or losing data when upgrading utf8 data from older versions of MySQL.

utf8mb4 is a superset of utf8.

总而言之,utf-8 只支持 3 个字节的字符;

如果是超过 4 个字节,那么就需要使用 utf8mb4 (max byte 4 ):加强版的 utf8 来存储

生僻字

查看生僻字占用的字节:

4 字节,显然 utf8 是不满足的

解决

提交 sql:

SQL 复制代码
alter table qt_check_answer default  CHARACTER SET utf8mb4

alt 之后还是报错;

查看库表:

表级别的设置更新了,但是字段还没有更新;

复现一下:



结论

此时更新字段级别的编码类型:解决

结论:MySQL编码存在字段级别的设置,生僻字需要字段级别的编码支持

相关推荐
zxcvb15310 分钟前
MySQL三种备份方式:原理、优缺点与实操选型指南
数据库·mysql
夜雪一千20 分钟前
PostgreSQL 自定义函数入门
数据库·postgresql
香蕉大香蕉(06敬畏市场)24 分钟前
事务隔离级别、MVCC 与锁:一条线串起来
java·服务器·数据库·人工智能·面试·博客
CoderYanger27 分钟前
Java EE 进阶:16. Spring原理(第一弹)
java·服务器·数据库·后端·spring·java-ee·github
开开心心就好40 分钟前
游戏安装报错缺环境?运行库整合包补上
java·mysql·游戏·pdf·ocr·音视频·lua
herinspace1 小时前
管家婆实用帖-SQL 2000如何修改混合模式
服务器·数据库·sql·管家婆软件·合肥管家婆软件·安徽功长网络科技有限公司
霸道流氓气质1 小时前
分布式序列号生成:基于 Redis 原子自增的编号方案
数据库·redis·分布式
Yuzhiyuxia1 小时前
【AI Agent 面试系列】LangGraph 设计思想与基础概念
数据库
Pniubi1 小时前
MVCC实战应用
数据库·mvcc
z2014z2 小时前
信息安全工程师 第7章 访问控制技术原理与应用 笔记
服务器·数据库·oracle