PostgreSQL - 错误:编码“UTF8”的字节序列无效:0x00
目前,我们在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能将零字节存储为 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 dataIf you want to stick with
text
, you should also figure out what the encoding of the file is.如果您必须使用
文本
类型,则可以将其保存为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.
您可以使用
decode
函数将二进制值(包括十六进制 0)插入到text
列中:You can use the
decode
function to insert binary values, including hex 0, into atext
column: