如何使用 postgresql 在 "bytea" 数据类型列中查询?

How to query inside "bytea" data type column with postgresql?

我正在尝试在具有字节类型的变量之间使用 AND 运算符。下面的错误困扰着我。

sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedFunction) operator does not exist: bytea & bytea
LINE 3: WHERE (doctors.schedule & '\x0000000000000000000000000000000...
                                ^
HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.

假设这两个变量是:

v = b'\x00\x00\x00\x03\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

c = b'\xff\xff\xc0\x03\xff\xff\xff\xff\xfc\x03\xff\xfe\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

知道如何解决这个问题吗? 提前表示感谢。

bytea 没有 & 运算符。毕竟bytea代表的是“字节数组”,而不是“位数组”。

但是,有一个函数可以测试某个位是否已设置:

SELECT get_bit('\xF0'::bytea, 4);

 get_bit 
═════════
       1
(1 row)

这将得到从低端开始第五位的值:11110000

如果要访问值的各个位,应使用数据类型 bit varying。这也将为您提供您期望的按位运算:

SELECT B'11110000' & B'00011000';

 ?column? 
══════════
 00010000
(1 row)

B 之前的字符串文字是 bit varying 文字。)