讓MySql支援Emoji表情(MySQL中4位元組utf8字元儲存方法)

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   使用   ar   java   for   

手機端插入Emoji表情,儲存到資料庫時報錯:

Caused by: java.sql.SQLException: Incorrect string value: ‘\xF0\x9F\x98\x84‘ for column ‘review‘ at row 1    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2734)    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458)

 

經檢查原因:

UTF-8編碼有可能是兩個、三個、四個位元組。Emoji表情是4個位元組,而Mysql的utf8編碼最多3個位元組,所以資料插不進去。

解決方案:將Mysql的編碼從utf8轉換成utf8mb4。

 

1. 修改my.ini [mysqld] character-set-server=utf8mb42. 在Connector/J的串連參數中,不要加characterEncoding參數。 不加這個參數時,預設值就時autodetect。3. 將已經建好的表也轉換成utf8mb4 
命令:ALTER TABLE `TABLE_NAME` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; (將TABLE_NAME替換成你的表名)4. 將需要使用emoji的欄位設定類型為:
命令:ALTER TABLE `TABLE_NAME`MODIFY COLUMN `COLUMN_NAME` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

 

參考:

http://stackoverflow.com/questions/2108824/mysql-incorrect-string-value-error-when-save-unicode-string-in-django

http://dev.mysql.com/doc/refman/5.5/en/connector-j-reference-charsets.html

讓MySql支援Emoji表情(MySQL中4位元組utf8字元儲存方法)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.