MySQL插入和unicode字符

时间:2013-07-08 11:02:36

标签: mysql database unicode encoding mysqldump

以下是示例代码:

CREATE DATABASE test_unicode DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;

USE test_unicode;

CREATE TABLE simple_table (text varchar(100) NOT NULL);

INSERT INTO simple_table VALUES ('l\u2019agave');

SELECT * FROM simple_table;

这是输出:

+-------------+
| text        |
+-------------+
| lu2019agave |
+-------------+
1 row in set (0.00 sec)

Mysql存储了我的字符串,没有 - “ \

我需要用“ \ ”来获取“l \ u2019agave”

我怎么做?

我为mysql尝试了很多编码设置,但它对我不起作用......

感谢您的建议!

3 个答案:

答案 0 :(得分:4)

尝试

INSERT INTO simple_table VALUES ('l\\u2019agave');

答案 1 :(得分:1)

试试这个:

INSERT INTO `test_unicode`.`simple_table` (`text`) VALUES ('l\\u2019agave'); 

This is because MySQL uses C escape syntax in strings (for example, “\n” to represent a newline character), you must double any “\” that you use in LIKE strings. For example, to search for “\n”, specify it as “\n”. To search for “\”, specify it as “\\”; this is because the backslashes are stripped once by the parser and again when the pattern match is made, leaving a single backslash to be matched against

答案 2 :(得分:0)

for java: org.apache.commons.lang3.StringEscapeUtils.unescapeJava(str); 来自Convert escaped Unicode character back to actual character