emoji就是我们聊天的时候的特殊表情, 是特殊字符(非字符串), unicode编码起始为 1F600 , 占用4个字节, 不同的终端显示可能不同,但是都是表示的同一个对象.
比如 "草莓" 这个表情, 在浏览器上效果如下
🍓
但是在微信上效果如下
在mysql workbench上效果如下(作为字符)
emoji完整表情可以查看: https://unicode.org/emoji/charts/full-emoji-list.html
命令行终端不支持emoji表情显示, 所以我使用的jupyter notebook
你可以直接复制其它地方的表情到你的python代码
print("🍓")
但是这样显然不方便, 所以我们可以调用emoji库来使用emoji
pip install emoji #安装emoji库
查看下emoji的类型和编码
import emoji
strawberry = emoji.emojize(':strawberry:')
cat = emoji.emojize(':grinning_cat:')
print(strawberry,cat)
print(type(strawberry), type(cat))
print(strawberry.encode(), cat.encode())
发现emoji是字符串类型, 编码是4字节.
通过上面发现emoji是字符串(这跟python语言有关, 实际上是字符), 占用4个字节, 所以得使用 utf8mb4 字符集(mysql低版本默认为utf8mb3)
mysql建表如下
CREATE TABLE `t20221125_emoji` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`emoji_char` varchar(1) DEFAULT NULL,
`emoji_str` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
emoji_char 字段存储单个emoji字符
emoji_str 字段存储含emoji的字符串
python代码如下(其它语言也同理):
import pymysql,emoji
conn = pymysql.connect(
host='192.168.101.21',
port=3308,
user='root',
password='123456',
);
cursor = conn.cursor()
sql1 = "insert into db1.t20221125_emoji(emoji_char,emoji_str) values('{strawberry}','i like {cat} 啊哈')".format(strawberry=emoji.emojize(':strawberry:'),cat=emoji.emojize(':grinning_cat:'))
sql2 = "insert into db1.t20221125_emoji(emoji_char,emoji_str) values('{cat}','i like {strawberry} 啊哈')".format(strawberry=emoji.emojize(':strawberry:'),cat=emoji.emojize(':grinning_cat:'))
print(sql1)
print(sql2)
cursor.execute(sql1)
cursor.execute(sql2)
conn.commit()
比如我想取出emoji_char=🍓的数据行, 可以这样写sql
select * from db1.t20221125_emoji where emoji_char='🍓';
但是我想找出emoji_str含有🍓的数据行使用like的时候发现并不行
select * from db1.t20221125_emoji where emoji_str like '%🍓%';
那是因为默认排序字符集的原因.这里就不再讲了.自己去探索吧😋
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。