如何在 MySQL 中将 BLOB 转换为 TEXT?

发布于 2024-07-22 05:59:08 字数 97 浏览 11 评论 0原文

我有大量记录,其中文本存储在 MySQL 的 blob 中。 为了便于处理,我想将数据库中的格式更改为文本...有什么想法可以轻松地进行更改以免中断数据 - 我想它需要正确编码?

I have a whole lot of records where text has been stored in a blob in MySQL. For ease of handling I'd like to change the format in the database to TEXT... Any ideas how easily to make the change so as not to interrupt the data - I guess it will need to be encoded properly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

星星的軌跡 2024-07-29 05:59:08

那是不必要的。 只需使用 SELECT CONVERT(column USING utf8) FROM... 而不仅仅是 SELECT column FROM...

That's unnecessary. Just use SELECT CONVERT(column USING utf8) FROM..... instead of just SELECT column FROM...

孤独患者 2024-07-29 05:59:08

以下是一个想要转换blob to char(1000) with UTF-8 编码:

CAST(a.ar_options AS CHAR(10000) CHARACTER SET utf8)

这是他的答案。 您可能可以在这里阅读更多有关 CAST 的内容。 我希望它能帮助一些人。

Here's an example of a person who wants to convert a blob to char(1000) with UTF-8 encoding:

CAST(a.ar_options AS CHAR(10000) CHARACTER SET utf8)

This is his answer. There is probably much more you can read about CAST right here. I hope it helps some.

杀手六號 2024-07-29 05:59:08

我遇到了同样的问题,这是我的解决方案:

  1. 在表中为每个 blob 列创建文本类型的新列
  2. 将所有 blob 转换为文本并将它们保存在新列中
  3. 删除 blob 列
  4. 将新列重命名为名称被移除的
ALTER TABLE mytable 
  添加列 field1_new TEXT NOT NULL, 
  添加列 field2_new TEXT NOT NULL; 

  更新 mytable 集 
  field1_new = CONVERT(field1 使用 utf8), 
  field2_new = CONVERT(field2 使用 utf8); 

  更改表 mytable 
  删除列字段1, 
  删除列字段2; 

  更改表 mytable 
  更改列 field1_new field1 文本, 
  更改列 field2_new field2 文本; 
  

I have had the same problem, and here is my solution:

  1. create new columns of type text in the table for each blob column
  2. convert all the blobs to text and save them in the new columns
  3. remove the blob columns
  4. rename the new columns to the names of the removed ones
ALTER TABLE mytable
ADD COLUMN field1_new TEXT NOT NULL,
ADD COLUMN field2_new TEXT NOT NULL;

update mytable set
field1_new = CONVERT(field1 USING utf8),
field2_new = CONVERT(field2 USING utf8);

alter table mytable
drop column field1,
drop column field2;

alter table mytable
change column field1_new field1 text,
change column field2_new field2 text;
为你拒绝所有暧昧 2024-07-29 05:59:08

如果您使用MYSQL-WORKBENCH,那么您可以正常选择blob列,右键单击列,然后单击在编辑器中打开值。 参考截图:

截图

If you are using MYSQL-WORKBENCH, then you can select blob column normally and right click on column and click open value in editor. refer screenshot:

screenshot

Spring初心 2024-07-29 05:59:08

这些答案都不适合我。 转换为 UTF8 时,当编码器遇到无法转换为 UTF8 的一组字节时,将导致 ? 替换会导致数据丢失。 您需要使用 UTF16:

SELECT
    blobfield,
    CONVERT(blobfield USING utf16),
    CONVERT(CONVERT(blobfield USING utf16), BINARY),
    CAST(blobfield  AS CHAR(10000) CHARACTER SET utf16),
    CAST(CAST(blobfield  AS CHAR(10000) CHARACTER SET utf16) AS BINARY)

您可以在 MySQL Workbench 中检查二进制值。 右键单击该字段 -> 在查看器中打开值-> 二进制。 当转换回 BINARY 时,二进制值应与原始值相同。

或者,您可以只使用为此目的而制作的 base-64:

SELECT
    blobfield,
    TO_BASE64(blobfield),
    FROM_BASE64(TO_BASE64(blobfield))

None of these answers worked for me. When converting to UTF8, when the encoder encounters a set of bytes it can't convert to UTF8 it will result in a ? substitution which results in data loss. You need to use UTF16:

SELECT
    blobfield,
    CONVERT(blobfield USING utf16),
    CONVERT(CONVERT(blobfield USING utf16), BINARY),
    CAST(blobfield  AS CHAR(10000) CHARACTER SET utf16),
    CAST(CAST(blobfield  AS CHAR(10000) CHARACTER SET utf16) AS BINARY)

You can inspect the binary values in MySQL Workbench. Right click on the field -> Open Value in Viewer-> Binary. When converted back to BINARY the binary values should be the same as the original.

Alternatively, you can just use base-64 which was made for this purpose:

SELECT
    blobfield,
    TO_BASE64(blobfield),
    FROM_BASE64(TO_BASE64(blobfield))
忆沫 2024-07-29 05:59:08

你可以很容易地做到。

ALTER TABLE `table_name` CHANGE COLUMN `column_name` `column_name` LONGTEXT NULL DEFAULT NULL ;

上面的查询对我有用。 我希望它也对你有帮助。

You can do it very easily.

ALTER TABLE `table_name` CHANGE COLUMN `column_name` `column_name` LONGTEXT NULL DEFAULT NULL ;

The above query worked for me. I hope it helps you too.

农村范ル 2024-07-29 05:59:08

phpMyAdmin 截图使用 phpMyAdmin 您还可以设置显示 BLOB 内容和显示完整文本的选项。

phpMyAdmin screenshotUsing phpMyAdmin you can also set the options to show BLOB content and show complete text.

偏闹i 2024-07-29 05:59:08

或者你可以使用这个功能:

DELIMITER $

CREATE FUNCTION BLOB2TXT (blobfield VARCHAR(255)) RETURNS longtext
DETERMINISTIC
NO SQL
BEGIN
       RETURN CAST(blobfield AS CHAR(10000) CHARACTER SET utf8);
END
$


DELIMITER ;

Or you can use this function:

DELIMITER $

CREATE FUNCTION BLOB2TXT (blobfield VARCHAR(255)) RETURNS longtext
DETERMINISTIC
NO SQL
BEGIN
       RETURN CAST(blobfield AS CHAR(10000) CHARACTER SET utf8);
END
$


DELIMITER ;
一刻暧昧 2024-07-29 05:59:08

我的 MariaDB 记录也有同样的问题。
(由我的同事)使用解决了

select
uncompress(blobfield)
from table

I had the same issue with my MariaDB records.
It was solved (by my colleague) using

select
uncompress(blobfield)
from table
迟到的我 2024-07-29 05:59:08
SELECCT TO_BASE64(blobfield)  
FROM the Table

为我工作。

CAST(blobfield AS CHAR(10000) CHARACTER SET utf8) 和 CAST(blobfield AS CHAR(10000) CHARACTER SET utf16) 没有显示我想要获取的文本值。

SELECCT TO_BASE64(blobfield)  
FROM the Table

worked for me.

The CAST(blobfield AS CHAR(10000) CHARACTER SET utf8) and CAST(blobfield AS CHAR(10000) CHARACTER SET utf16) did not show me the text value I wanted to get.

一紙繁鸢 2024-07-29 05:59:08

我不明白为什么不那么容易:

UUID_TO_BIN('77dea2ad-3c8c-40c6-a278-7cf1a1ac9384')

BIN_TO_UUID(FIELD)

i cannot understand why not so easy:

UUID_TO_BIN('77dea2ad-3c8c-40c6-a278-7cf1a1ac9384')

and

BIN_TO_UUID(FIELD)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文