如何在 MySQL 中将 BLOB 转换为 TEXT?
我有大量记录,其中文本存储在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
那是不必要的。 只需使用
SELECT CONVERT(column USING utf8) FROM
... 而不仅仅是SELECT column FROM
...That's unnecessary. Just use
SELECT CONVERT(column USING utf8) FROM
..... instead of justSELECT column FROM
...以下是一个想要转换blob to char(1000) with UTF-8 编码:
这是他的答案。 您可能可以在这里阅读更多有关 CAST 的内容。 我希望它能帮助一些人。
Here's an example of a person who wants to convert a blob to char(1000) with UTF-8 encoding:
This is his answer. There is probably much more you can read about CAST right here. I hope it helps some.
我遇到了同样的问题,这是我的解决方案:
I have had the same problem, and here is my solution:
如果您使用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:
这些答案都不适合我。 转换为 UTF8 时,当编码器遇到无法转换为 UTF8 的一组字节时,将导致 ? 替换会导致数据丢失。 您需要使用 UTF16:
您可以在 MySQL Workbench 中检查二进制值。 右键单击该字段 -> 在查看器中打开值-> 二进制。 当转换回 BINARY 时,二进制值应与原始值相同。
或者,您可以只使用为此目的而制作的 base-64:
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:
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:
你可以很容易地做到。
上面的查询对我有用。 我希望它也对你有帮助。
You can do it very easily.
The above query worked for me. I hope it helps you too.
使用 phpMyAdmin 您还可以设置显示 BLOB 内容和显示完整文本的选项。
Using phpMyAdmin you can also set the options to show BLOB content and show complete text.
或者你可以使用这个功能:
Or you can use this function:
我的 MariaDB 记录也有同样的问题。
(由我的同事)使用解决了
I had the same issue with my MariaDB records.
It was solved (by my colleague) using
为我工作。
CAST(blobfield AS CHAR(10000) CHARACTER SET utf8) 和 CAST(blobfield AS CHAR(10000) CHARACTER SET utf16) 没有显示我想要获取的文本值。
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.
我不明白为什么不那么容易:
和
i cannot understand why not so easy:
and