MySQL 插入和 unicode 字符

问题描述 投票:0回答:4

这是示例代码:

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的编码设置,但它对我不起作用......

谢谢您的建议!

database unicode encoding mysql
4个回答
4
投票

尝试

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

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
.


0
投票

对于java:

org.apache.commons.lang3.StringEscapeUtils.unescapeJava(str);
将转义的 Unicode 字符转换回实际字符


0
投票

从今天(2024 年)开始,可以在插入 sql 中使用此转义语法。此示例使用 utf8mb4 字符集。

文本值为

MyText ABCÅÄÖ 😀𝅘𝅥𝅮📫

INSERT INTO table (id,textvalue) 
VALUES (1, Concat('MyText ABCÅÄÖ ', char(0xF09F9880 USING utf8mb4), char(0xf09d85a0 USING utf8mb4), char(0xF09F93AB USING utf8mb4)) );

utf8 十六进制取自此站点。 https://decodeunicode.org/en/u+1F600

© www.soinside.com 2019 - 2024. All rights reserved.