mysql數據庫如何支持emoji表情

分類:存儲 時間:2017-02-22

     mysql數據庫的默認字符集utf8,只能存儲3個字節的數據。標準的emoji表情是4個字節,在APP端輸入保存表情是用戶的普遍需求和行為。

 

   插入數據庫報錯如下:

   Java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\xAA",...' for column 'raw_json' at row 1, 異常:org.springframework.jdbc.UncategorizedSQLException: 


   解決方式:更換字符集utf8-->utf8mb4      mb4的意思是most bytes 4,專門為兼容四個字節的。utf8mb4是向下兼容utf8的,所以即便修改了字段的字符集也不會影響線上數據。

   前提:mysql大於5.5.3版本。

   步驟:

 1.修改字段的字符集

     ALTER table mb_touchpay_record  modify  clientName  varchar(100) character set utf8mb4 collate utf8mb4_unicode_ci

 2.表

     ALTER table mb_touchpay_record charset=utf8mb4;

 3.庫

     set names utf8mb4

    修改後可看到字符集設置:

   

mysql> SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';  
+--------------------------+--------------------+  
| Variable_name            | Value              |  
+--------------------------+--------------------+  
| character_set_client     | utf8mb4            |  
| character_set_connection | utf8mb4            |  
| character_set_database   | utf8mb4            |  
| character_set_filesystem | binary             |  
| character_set_results    | utf8mb4            |  
| character_set_server     | utf8mb4            |  
| character_set_system     | utf8               |  
| collation_connection     | utf8mb4_unicode_ci |  
| collation_database       | utf8mb4_unicode_ci |  
| collation_server         | utf8mb4_unicode_ci |  
+--------------------------+--------------------+  
 rows in set (0.00 sec) 

  不需要重啟數據庫


 4.如果以上步驟操作後還不能保存成功,連接數據庫時設置字符集如下:

              

  //保存備註emoji表情
  Query mb4_q = dao.createSQLQuery("set names utf8mb4", null);
  mb4_q.executeupdate();


  

 


Tags:

文章來源:


ads
ads

相關文章
ads

相關文章

ad