aotuman97
aotuman97
aotuman97
Build reliable and efficient software.
Joined 3 years ago
·https://github.com/aotuman97
JuneJulyAugSeptOctNovDecJanFebMarAprMay
aotuman97
·

理论上 UTF-8 使用1~6个字节进行存储,但比较新的 UTF-8 规范只使用 1~4 个字节

基本多文平面(BMP): 三个字节的 UTF-8 最大能编码的 Unicode 字符是 0xFFFF,也就是 Unicode 中的基本多文平面(BMP)。也就是说,任何不在基本多文平面的 Unicode字符,都无法用三字节编码存储。

不在 BMP 中的字符: Emoji 表情(Emoji 是一种特殊的 Unicode 编码,常见于 ios 和 android 手机上)、不常用的汉字、新增的 Unicode 字符等等。

utf8: 等同于utf8mb3,低版本mysql的默认存储格式,最大字符长度为 3 字节,如果遇到 4 字节的字符就会出现错误了。如上述的Emoji表情

utf8mb4: MySQL在5.5.3版本之后增加了这个utf8mb4的编码,mb4就是most bytes 4的意思,专门用来兼容四字节的unicode。其实,utf8mb4是utf8的超集,原来使用utf8,然后将字符集修改为utf8mb4,也会不会对已有的 utf8 编码读取产生任何问题。

可以用 alter 命令解决:

# 修改相应表
ALTER TABLE logtest CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
# 设置默认编码
ALTER TABLE logtest DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
# 只修改部分字段编码
ALTER TABLE logtest CHANGE title title VARCHAR(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

如果还是报错,可以修改数据库编码方式:

# 查看编码
show variables like '%char%';

# set names进行设置改变三个参数
# character_set_client
# character_set_connection
# character_set_results
set names utf8mb4;

# 或者使用下面命令一个一个更改
set character_set_client = 'utf8mb4';
set character_set_connection = 'utf8mb4';
set character_set_databse = 'utf8mb4';
set character_set_results = 'utf8mb4';

其中 character_set_server 无法通过命令修改,需要修改 mysql 配置文件,然后重启 mysql 才能生效。

grin 以前记录的笔记,抄给你

aotuman97
·

Public

aotuman97
·

引用是故意这样做的:Can toml use variables?

It can't, and that was a deliberate design choice. Any logic, complicated or not, is best handled by the consumer of the TOML file. TOML doesn't handle that logic.