MySQL将Unicode解码为UTF

编程入门 行业动态 更新时间:2024-10-08 20:40:02
本文介绍了MySQL将Unicode解码为UTF-8函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

插入表时,我想将Unicode字符串解码为UTF-8.这是我所拥有的:

I want to decode Unicode strings to UTF-8 when inserting in a table. Here's what I have:

('\u0645\u064e\u062b\u0652\u0646\u064e\u0649 \u00a0\u062c \u0645\u064e\u062b\u064e\u0627\u0646\u064d')

因此,我希望将这些值转换为UTF-8,例如:

So I want these values to be converted to UTF-8, for example:

INSERT INTO `nouns`(`NOUNID`, `WORDID`, `SINGULAR`, `PLURAL`) VALUES (781, 3188, '\u0646\u064e\u062c\u0652\u0645', ('\u0646\u064e\u062c\u0652\u0645'))

我正在将h2数据库迁移到MySQL,因此在编写h2数据库脚本时得到了此提示:

I am migrating my h2 database to MySQL, so I got this when scripting my h2 db:

INSERT INTO PUBLIC.NOUNS(NOUNID, WORDID, SINGULAR, PLURAL) VALUES (1, 5, STRINGDECODE('\u0623\u0628\u0651 '), STRINGDECODE ('\u0623\u0624\u064f\u0628\u0651')), (2, 9, STRINGDECODE('\u0623\u064e\u0628\u0627\u0628'), ''),

基本上,thees \ u0623 \ u0632 \ u0651是unicode表示形式的阿拉伯字符,我想将它们转换为真实的阿拉伯字符,以便像在数据库中那样存储. 我正在尝试转换函数,但是因为我是mysql的新手,所以我无法实现这一点:

basicly thees \u0623\u0632\u0651 are arabic charchters in unicode representation and I want to convert them in real arabic characters, to be stored like that in database. I was trying convert function, but because I am new to mysql I couldn't achive this:

SELECT CONVERT(_ucs2'\u0623' USING utf8);

推荐答案

在MySQL中,没有内置函数可以解码unicode转义,但是您可以创建一个.见下文:

There is no built-in function to decode unicode escapes in MySQL, but you can create one; see below:

请注意,反斜杠是MySQL中的转义字符,因此在编写SQL时需要将它们加倍:'\\u0623\\u064e\\u0628\\u0627\\u0628'

Note that the backslash is an escape character in MySQL, so you'll need to double them when you write SQL: '\\u0623\\u064e\\u0628\\u0627\\u0628'

DELIMITER // CREATE FUNCTION STRINGDECODE(str TEXT CHARSET utf8) RETURNS text CHARSET utf8 DETERMINISTIC BEGIN declare pos int; declare escape char(6) charset utf8; declare unescape char(3) charset utf8; set pos = locate('\\u', str); while pos > 0 do set escape = substring(str, pos, 6); set unescape = char(conv(substring(escape,3),16,10) using ucs2); set str = replace(str, escape, unescape); set pos = locate('\\u', str, pos+1); end while; return str; END// DELIMITER ;

更多推荐

MySQL将Unicode解码为UTF

本文发布于:2023-11-05 10:17:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560561.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:MySQL   Unicode   UTF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!