EmojiEmoji錄入MySQL資料庫報錯的解決方案,emojimysql

來源:互聯網
上載者:User

EmojiEmoji錄入MySQL資料庫報錯的解決方案,emojimysql

前言:手機app應用評論的時候,恢複Emoji,提示失敗。

1,查看tomcat後台日誌,核心報錯資訊如下: 
 Caused by: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x97\xF0\x9F...' for column 'CONTENT' 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.execute(PreparedStatement.java:1379)
at org.apache.commons.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:172)
at org.apache.commons.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:172)
at com.ibatis.sqlmap.engine.execution.SqlExecutor.executeUpdate(SqlExecutor.java:80)
at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.sqlExecuteUpdate(MappedStatement.java:216)
at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeUpdate(MappedStatement.java:94)
... 46 more 
 
[root@mysqlvm2 ~]# mysql -root -p
mysql> use test;
Database changed
mysql> show tables;
Ignoring query to other database
mysql> Ctrl-C -- exit!
Aborted
忘記輸入-u參數了,沖洗你輸入,OK,如下所示:
[root@mysqlvm2 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.12-log Source distribution
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>  
mysql> use test;
Database changed
mysql> show tables;
+------------------------+
| Tables_in_test         |
+------------------------+
| c                      | 
| lubin_test             | 
| test                   | 
| tt                     | 
+------------------------+
10 rows in set (0.00 sec)
這種輸入Incorrect string value: '\xF0\x9F\x98\x97\xF0\x9F...'的問題,多數都是字元集的,以前從latain改成gbk,從gbk改成utf8,而我的CONTENT欄位已經utf8了,那麼比utf8更多的就只有utf8mb4了,所以去修改表欄位的字元集吧。

mysql>


2,先去修改表欄位字元集為utf8mb4: 
ALTER TABLE UGC_REVIEW_CONTENT MODIFY `CONTENT` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '評論內容';
執行完後,再在手機端的app測試,依然報同樣的錯誤。


3,再去修改表字元集utf8mb4:
ALTER TABLE UGC_REVIEW_CONTENT  CHARSET=utf8mb4 COMMENT='曬單/推薦的評論內容';
執行完後,再在手機端的app測試,依然報同樣的錯誤。


4,再去修改資料庫的字元集utf8mb4: 
vim my.cnf
init-connect='SET NAMES utf8mb4'
character-set-server=utf8mb4

重啟mysql資料庫
[root@mysqlvm4 ~]# service mysql restart
Shutting down MySQL....                                    [確定]
Starting MySQL.............................................[確定]..
[root@mysqlvm4 ~]# 

查看db的字元集 
mysql> show variables like '%char%';
+--------------------------+----------------------------------+
| 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                             |
| character_sets_dir       | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)
mysql> 

在手機app端重新評論,輸入Emoji,點擊提交提示評論成功。

5,問題分析總結:
    ㈠ 原因
        普通的字串或者表情都是佔位3個位元組,所以utf8足夠用了,但是移動端的Emoji佔位是4個位元組,普通的utf8就不夠用了,為了應對無線互連網的機遇和挑戰、避免 emoji Emoji帶來的問題、涉及無線相關的 MySQL 資料庫建議都提前採用 utf8mb4 字元集,這必須要作為移動互連網行業的一個技術選型的要點
    ㈡ 限制
        需要 >= MySQL 5.5.3版本、從庫也必須是5.5的了、低版本不支援這個字元集、複製報錯。

參考文章地址:http://bbs.csdn.net/topics/390055415
怎將mysql資料庫報錯資訊隱藏?不讓它在頁面顯示

birthday 欄位 顯示是一個日期格式的

ada 這個值根本不是哇

把'ada' 換成 now()
 
mysql建立資料庫報錯

windos鍵+r 輸入services.msc 查看mysql 服務開了沒有
 

相關文章

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.