MySQL 存储 Emoji #22
Labels
blog: programming
@post
This is a blog post
tag: mysql
|132950002-15455ec6-a178-4488-845b-94e97243f3e3.jpg
View Post on Blog
This post was originally created at 2019-12-23T22:21:24+08:00
UPDATE: See https://docs.sqlalchemy.org/en/13/dialects/mysql.html#unicode for more info
utf8_bin
?一开始就套用存储中文姓名的那一套,使用
utf8_bin
的 collation,觉得 utf8 这种万能的东西直接用就行了。可谁知给我报错:蛤?竟有如此操作?
utf8mb4_unicode_ci
?一波搜索之后就看到了使用
utf8mb4_unicode_ci
的 collation。于是就写:可惜并没有实质性效果
暴力二进制
utf8 解决不了,二进制存储总行了吧?
果然,存进去并没有问题。但是当要读取出来的时候又报了奇怪的错误:
明明是二进制怎么给我搞出来一个编码问题?
后期查阅资料发现,SQL 似乎有一个动态类型,每列的数据类型是建议值,并不强制。其结果就是我存一个二进制编码的字符串,他就真的以为是字符类型,并且数据库表里没有存编码,导致了问题出现。
疾病设计:angry:!!!
直接escape
二进制也不行,全 escape 总可以了吧!
总算成功存储和读取了!
人人都说
utf8mb4_unicode_ci
好为什么就是不行呢?
哦,还要改数据库 charset, collation
还要给 SQLAlchemy 加
?charset=utf8mb4
可是为什么还是不行?
我几几年用过这编码?
版本大坑
我找到了
mysql-connector-python
的官方文档:https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.htmlcollation 一栏中:
什么?8.0 的默认值?我们只有 5.7 呢!
虽然并不是
utf8mb4_0900_ai_ci
这种东西,我还是试着把链接数据库参数修改成utf8mb4_unicode_ci
。噫!成功了!
事实证明
utf8mb4_0900_ai_ci
和utf8mb4_general_ai_ci
应该是同义词一样的存在。可是:
好一个
recommended
,就给我挖这种坑?版本问题害死人!The text was updated successfully, but these errors were encountered: