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

相關文章
相關標籤/搜索