Postgresql 不接受 UTF 字符串中的 \0,而 C# 接受
Postgresql does not accept \0 in UTF string, while C# does
我有一些包含 [=11=]
字节的数据,这似乎是有效的 UTF8 数据:
using System;
using System.Text;
public class Program
{
public static void Main()
{
byte[] b = new byte[3];
b[0] = 65;
b[1] = 66;
b[2] = 0;
Console.WriteLine(Encoding.UTF8.GetString(b));
}
}
那个代码works fine。但是,当尝试更新 Postgres 中的记录时,它会抱怨:
22021: invalid byte sequence for encoding "UTF8": 0x00
数据不应该存在,但怎么会一个系统接受,另一个系统不接受呢?我估计他们都执行标准。
来自文档
8.3.字符类型
+-----------------------------------+----------------------------+
| Name | Description |
+-----------------------------------+----------------------------+
| character varying(n), varchar(n) | variable-length with limit |
| character(n), char(n) | fixed-length, blank padded |
| text | variable unlimited length |
+-----------------------------------+----------------------------+
The characters that can be stored in any of these data types are determined by the database character set, which is selected when the database is created. Regardless of the specific character set, the character with code zero (sometimes called NUL) cannot be stored. For more information refer to Section 23.3.
我有一些包含 [=11=]
字节的数据,这似乎是有效的 UTF8 数据:
using System;
using System.Text;
public class Program
{
public static void Main()
{
byte[] b = new byte[3];
b[0] = 65;
b[1] = 66;
b[2] = 0;
Console.WriteLine(Encoding.UTF8.GetString(b));
}
}
那个代码works fine。但是,当尝试更新 Postgres 中的记录时,它会抱怨:
22021: invalid byte sequence for encoding "UTF8": 0x00
数据不应该存在,但怎么会一个系统接受,另一个系统不接受呢?我估计他们都执行标准。
来自文档 8.3.字符类型
+-----------------------------------+----------------------------+ | Name | Description | +-----------------------------------+----------------------------+ | character varying(n), varchar(n) | variable-length with limit | | character(n), char(n) | fixed-length, blank padded | | text | variable unlimited length | +-----------------------------------+----------------------------+
The characters that can be stored in any of these data types are determined by the database character set, which is selected when the database is created. Regardless of the specific character set, the character with code zero (sometimes called NUL) cannot be stored. For more information refer to Section 23.3.