### Error updating database. Cause: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\xBF3' for column 'name' at row 1 ### The error may exist in com/youxuepai/knowledge/mapper/audit/StdKnowledgeChapterMapper.java (best guess) ### The error may involve com.youxuepai.knowledge.mapper.audit.StdKnowledgeChapterMapper.insert-Inline ### The error occurred while setting parameters ### SQL: INSERT INTO std_knowledge_chapter ( chapter_id, p_chapter_id, object_id, name, level, rank, created_by, created, updated_by, updated ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) ### Cause: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\xBF3' for column 'name' at row 1 ; uncategorized SQLException; SQL state [HY000]; error code [1366]; Incorrect string value: '\xF0\x9F\x98\xBF3' for column 'name' at row 1; nested exception is java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\xBF3' for column 'name' at row 1"java
這個問題,緣由是UTF-8編碼有多是兩個、三個、四個字節。Emoji表情或者某些特殊字符是4個字節,而MySQL的utf8編碼最多3個字節,因此數據插不進去。mysql
個人解決方案是這樣的sql
[mysqld]數據庫
character-set-server=utf8mb4windows
[mysql]app
default-character-set=utf8mb4編碼
修改後重啓Mysqlspa
命令:.net
更改數據庫編碼:ALTER DATABASE caitu99 CHARACTER SET utf8mb4
COLLATE utf8mb4_general_ci
;code
alter table TABLE_NAME convert to character set utf8mb4 collate utf8mb4_bin; (將TABLE_NAME替換成你的表名)
而後就OK了。網上流傳的一個版本增長了一個步驟,就是把mysql環境變量將character_set_client,character_set_connection,character_set_database,character_set_results,character_set_server 都修改爲utf8mb4,不過我沒有作這一步,也正常,因此多是這一步是多餘的,若是須要改,則按照下面介紹作修改。
my.cnf通常在etc/mysql/my.cnf位置。找到後請在如下三部分裏添加以下內容: [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'