本文實例分析了mysql存儲emoji表情報錯的處理方法。分享給大家供大家參考,具體如下:
utf-8編碼可能2個字節、3個字節、4個字節的字符,但是MySQL的utf8編碼只支持3字節的數據,而移動端的表情數據是4個字節的字符。如果直接往采用utf-8編碼的數據庫中插入表情數據,Java程序中將報SQL異常:
java.sql.SQLException: Incorrect string value: '/xF0/x9F/x92/x94' for column 'name' at row 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3593)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3525)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1986)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2140)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2620)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1662)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1581)
可以對4字節的字符進行編碼存儲,然后取出來的時候,再進行解碼。但是這樣做會使得任何使用該字符的地方都要進行編碼與解碼。
utf8mb4編碼是utf8編碼的超集,兼容utf8,并且能存儲4字節的表情字符。
采用utf8mb4編碼的好處是:存儲與獲取數據的時候,不用再考慮表情字符的編碼與解碼問題。
更改數據庫的編碼為utf8mb4:
1. MySQL的版本
utf8mb4的最低mysql版本支持版本為5.5.3+,若不是,請升級到較新版本。
2. MySQL驅動
5.1.34可用,最低不能低于5.1.13
3.修改MySQL配置文件
修改mysql配置文件my.cnf(windows為my.ini)
my.cnf一般在etc/mysql/my.cnf位置。找到后請在以下三部分里添加如下內容:
[client]default-character-set = utf8mb4[mysql]default-character-set = utf8mb4[mysqld]character-set-client-handshake = FALSEcharacter-set-server = utf8mb4collation-server = utf8mb4_unicode_ciinit_connect='SET NAMES utf8mb4'
4. 重啟數據庫,檢查變量
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 |
collation_connection 、collation_database 、collation_server是什么沒關系。
但必須保證
系統變量 | 描述 |
---|---|
character_set_client | (客戶端來源數據使用的字符集) |
character_set_connection | (連接層字符集) |
character_set_database | (當前選中數據庫的默認字符集) |
character_set_results | (查詢結果字符集) |
character_set_server | (默認的內部操作字符集) |
這幾個變量必須是utf8mb4。
5. 數據庫連接的配置
數據庫連接參數中:
characterEncoding=utf8
會被自動識別為utf8mb4,也可以不加這個參數,會自動檢測。
而autoReconnect=true
是必須加上的。
6. 將數據庫和已經建好的表也轉換成utf8mb4
更改數據庫編碼:
更改表編碼:
如有必要,還可以更改列的編碼
7、在第3步設置character_set_database,character_set_server不成功的可以試下直接在mysql.exe下
set @@character_set_server='utf8mb4';set @@character_set_database='utf8mb4';
這下數據庫就可以存下emoji表情的編碼了。
附上我的my.ini
# For advice on how to change settings please see# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the# *** default location during install, and will be replaced if you# *** upgrade to a newer version of MySQL.[client]default-character-set = utf8mb4[mysql]default-character-set = utf8mb4[mysqld]character-set-client-handshake = FALSEcharacter-set-server = utf8mb4collation-server = utf8mb4_unicode_ciinit_connect='SET NAMES utf8mb4'# Remove leading # and set to the amount of RAM for the most important data# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.# innodb_buffer_pool_size = 128M# Remove leading # to turn on a very important data integrity option: logging# changes to the binary log between backups.# log_bin# These are commonly set, remove the # and set as required.# basedir = .....# datadir = .....# port = .....# server_id = .....# Remove leading # to set options mainly useful for reporting servers.# The server defaults are faster for transactions and fast SELECTs.# Adjust sizes as needed, experiment to find the optimal values.# join_buffer_size = 128M# sort_buffer_size = 2M# read_rnd_buffer_size = 2Msql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
希望本文所述對大家MySQL數據庫計有所幫助。
新聞熱點
疑難解答