PostgreSQL中的unicode字符串是否允许通过Python使用空字节?

gue*_*tli 4 python postgresql unicode

unicode字符串中是否允许使用空字节?

我不问utf8,我的意思是unicode字符串的高级对象表示.

背景

我们在PostgreSQL中通过Python存储包含空字节的unicode字符串.

如果我们再次读取它,则字符串在空字节处切割.

Mat*_*sOl 7

关于数据库方面,PostgreSQL本身不允许'\0'在char/text/varchar字段的字符串中使用null byte(),因此如果您尝试存储包含它的字符串,则会收到错误.例:

postgres=# SELECT convert_from('foo\000bar'::bytea, 'unicode');
ERROR:  22021: invalid byte sequence for encoding "UTF8": 0x00
Run Code Online (Sandbox Code Playgroud)

如果您确实需要存储此类信息,那么您可以bytea在PostgreSQL端使用数据类型.确保正确编码.