本文實(shí)例分析了mysql存儲emoji表情報(bào)錯的處理方法。分享給大家供大家參考,具體如下:
utf-8編碼可能2個字節(jié)、3個字節(jié)、4個字節(jié)的字符,但是MySQL的utf8編碼只支持3字節(jié)的數(shù)據(jù),而移動端的表情數(shù)據(jù)是4個字節(jié)的字符。如果直接往采用utf-8編碼的數(shù)據(jù)庫中插入表情數(shù)據(jù),Java程序中將報(bào)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字節(jié)的字符進(jìn)行編碼存儲,然后取出來的時候,再進(jìn)行解碼。但是這樣做會使得任何使用該字符的地方都要進(jìn)行編碼與解碼。
utf8mb4編碼是utf8編碼的超集,兼容utf8,并且能存儲4字節(jié)的表情字符。
采用utf8mb4編碼的好處是:存儲與獲取數(shù)據(jù)的時候,不用再考慮表情字符的編碼與解碼問題。
更改數(shù)據(jù)庫的編碼為utf8mb4:
1. MySQL的版本
utf8mb4的最低mysql版本支持版本為5.5.3+,若不是,請升級到較新版本。
2. MySQL驅(qū)動
5.1.34可用,最低不能低于5.1.13
3.修改MySQL配置文件
修改mysql配置文件my.cnf(windows為my.ini)
my.cnf一般在etc/mysql/my.cnf位置。找到后請?jiān)谝韵氯糠掷锾砑尤缦聝?nèi)容:
[client] default-character-set = utf8mb4 [mysql] default-character-set = utf8mb4 [mysqld] character-set-client-handshake = FALSE character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci init_connect='SET NAMES utf8mb4'
4. 重啟數(shù)據(jù)庫,檢查變量
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是什么沒關(guān)系。
但必須保證
系統(tǒng)變量 | 描述 |
---|---|
character_set_client | (客戶端來源數(shù)據(jù)使用的字符集) |
character_set_connection | (連接層字符集) |
character_set_database | (當(dāng)前選中數(shù)據(jù)庫的默認(rèn)字符集) |
character_set_results | (查詢結(jié)果字符集) |
character_set_server | (默認(rèn)的內(nèi)部操作字符集) |
這幾個變量必須是utf8mb4。
5. 數(shù)據(jù)庫連接的配置
數(shù)據(jù)庫連接參數(shù)中:
characterEncoding=utf8
會被自動識別為utf8mb4,也可以不加這個參數(shù),會自動檢測。
而autoReconnect=true
是必須加上的。
6. 將數(shù)據(jù)庫和已經(jīng)建好的表也轉(zhuǎn)換成utf8mb4
更改數(shù)據(jù)庫編碼:
更改表編碼:
如有必要,還可以更改列的編碼
7、在第3步設(shè)置character_set_database,character_set_server不成功的可以試下直接在mysql.exe下
set @@character_set_server='utf8mb4'; set @@character_set_database='utf8mb4';
這下數(shù)據(jù)庫就可以存下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 = FALSE character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci init_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 = 2M sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
希望本文所述對大家MySQL數(shù)據(jù)庫計(jì)有所幫助。