Mysql失败,异常pymysql.err.InternalError: (1366, "Incorrect string value: '\\xF0\\x9D\\x90\\x...

问题描述:

  插入Mysql时失败了,python代码报以下异常:python

  pymysql.err.InternalError: (1366, "Incorrect string value: '\\xF0\\x9D\\x90\\xBF;......mysql

缘由分析:

  UTF-8编码有多是两个、三个、四个字节。Emoji表情是4个字节,而Mysql的utf8编码最多3个字节,因此数据插不进去。sql

解决方案:

  修改Mysql表的字符集和Pymysql链接库时的字符集数据库

  一、修改Mysql表的字符集

    说明:将已经建好的表字符集转改为 utf8mb4,排序规则改成 utf8mb4_bin编码

    命令:alter table TABLE_NAME convert to character set utf8mb4 collate utf8mb4_bin; (将TABLE_NAME替换成你的表名)spa

    注意:排序规则不是 utf8mb4_general_ci,而是utf8mb4_bin,不要想固然排序

 

  二、修改数据库链接的字符集        

    conn = pymysql.connect(host='localhost', user='root', password='root', port=3306, db='cncb', charset='utf8mb4')ci

相关文章
相关标签/搜索