我很难对我的mysql数据库进行编码。无法正确插入一些中文字符。我在Windows上使用xampp并拥有下表:
CREATE TABLE `Kanji` (
`ID` MEDIUMINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
`Kanji` VARCHAR(40) NOT NULL UNIQUE
);
以下插入不起作用:
INSERT INTO Kanji (Kanji)
VALUES ('');
当我在phpMyAdmin中检查表的内容时,它只显示一个'?'。插入其他汉字有效(例如'陆')。
以下是我的数据库的编码设置:
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_general_ci
collation_database | utf8mb4_general_ci
collation_server | utf8mb4_general_ci
任何想法有什么不对?
答案 0 :(得分:0)
这与utf8无法处理4字节字符有关,如下所述:
https://dev.mysql.com/doc/refman/5.7/en/charset-unicode-utf8mb4.html
要解决这个问题,我必须:
1)运行在评论部分找到的三个ALTER语句,标记为正确答案: "Incorrect string value" when trying to insert UTF-8 into MySQL via JDBC?
(ALTER语句如下所示,在我创建的测试用例中)
2)在MySQL Workbench中设置JDBC字符集: 来自https://dev.mysql.com/doc/refman/5.7/en/charset-connection.html (同样在下面的测试用例中)
以下是我通过MySQL Workbench创建的测试用例:
-- I was able to re-produce your case:
create table test.kanji ( col varchar(20) ) ;
INSERT INTO test.kanji (col) VALUES ('');
select * from test.kanji ;
------
?
---- The solution:
; -- From link 1:
ALTER DATABASE test CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_520_ci;
ALTER TABLE test.kanji CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci;
ALTER TABLE test.kanji modify col VARCHAR(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci ;
-- From link 2:
set names 'utf8mb4' ;
select * from test.kanji ;
---------
? <-- Interesting, this did not clean-up the data we inserted.
;
-- Insert a new row now that we are using utf8mb4 in the connection:
INSERT INTO test.kanji (col) VALUES ('');
select * from test.kanji ;
---------
? <-- The first insert - still "dirty"
<-- The second insert is correct.
-- In case you want to re-set everything back to utf8:
;
ALTER DATABASE test CHARACTER SET = utf8 COLLATE = utf8_general_ci;
ALTER TABLE test.kanji CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE test.kanji modify col VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci ;