PostgreSQL - 错误:编码“UTF8”的字节序列无效:0x00

发布于 2025-01-17 00:52:15 字数 309 浏览 4 评论 0原文

目前,我们在 PostgresSQL 中插入记录时遇到问题。我的数据类型是 TEXT 类型。它抛出一个错误,指出错误:编码“UTF8”的字节序列无效:0x00

我们尝试插入的数据包含 RTF 文本,其中包含文本、图像,后跟文本。我们还确保没有传递或插入空值。

我们使用 **PostgresSQL ** 版本 9.6 和 12,编码设置为 UTF-8。

任何帮助将不胜感激。

带有文本和图像(包含特殊字符)的 RTF 数据应该可以毫无问题地插入到 PostgresSQL 中。此外,数据类型应为 TEXT 类型。

Currently, we are facing an issue while inserting a record in PostgresSQL. My data-type is of type TEXT. It throws an error saying Error: invalid byte sequence for encoding "UTF8": 0x00.

The data that we are trying to insert contains RTF text which contains text, image followed by again text. We also made sure there are no null values passed or inserted.

We are using **PostgresSQL **version 9.6 and 12 with an encoding set as UTF-8.

Any help would be appreciated.

The RTF data with text and images (contains special characters) should insert into PostgresSQL without any issues. Also, the data type should be of type TEXT.

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

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

发布评论

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

评论(3

秋叶绚丽 2025-01-24 00:52:15

您不能将零字节存储为 PostgreSQL 中文本字符串的一部分。您有两个选择:

  • 如果不需要,请从输入字符串中删除该字符

  • 使用适合二进制数据的数据类型bytea

如果您想坚持使用text,您还应该弄清楚文件的编码是什么。

You cannot store a zero byte as part of a text string in PostgreSQL. You have two options:

  • remove this character from the input string if it is not required

  • use data type bytea, which is suitable for binary data

If you want to stick with text, you should also figure out what the encoding of the file is.

百合的盛世恋 2025-01-24 00:52:15

如果您必须使用文本类型,则可以将其保存为base64编码。

数据库中的空间开销约为原始数据的 30%,并且应用程序代码中需要少量的 CPU 对其进行编码/解码。

If you must use text type, you could save it base64 encoded.

There is a space overhead in the DB of about 30% over the raw data and a modest amount of CPU in your app code to encode/decode it.

記憶穿過時間隧道 2025-01-24 00:52:15

您可以使用 decode 函数将二进制值(包括十六进制 0)插入到 text 列中:

postgres=# create table jos(foo text);
CREATE TABLE
postgres=# insert into jos values('foo'||decode('00', 'hex')||'bar');
INSERT 0 1
postgres=# select * from jos;
     foo
------------------
 \x666f6f00626172
(1 row)

You can use the decode function to insert binary values, including hex 0, into a text column:

postgres=# create table jos(foo text);
CREATE TABLE
postgres=# insert into jos values('foo'||decode('00', 'hex')||'bar');
INSERT 0 1
postgres=# select * from jos;
     foo
------------------
 \x666f6f00626172
(1 row)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文