Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将十六进制文本字符串作为字节插入PostgreSQL_Sql_Postgresql - Fatal编程技术网

将十六进制文本字符串作为字节插入PostgreSQL

将十六进制文本字符串作为字节插入PostgreSQL,sql,postgresql,Sql,Postgresql,我有一个文本文件,其中有几个十六进制字符串: 013d7d16d7ad4fefb61bd95b765c8ceb 007687fc64b746569616414b78c81ef1 我想将这些存储在数据库中作为bytea,而不是varchar。也就是说,我希望数据库将01存储为单字节00000001,而不是字符“0”和“1” 我可以通过sed轻松运行此文件,以任何方式格式化/转义它 这就是我尝试过的: create table mytable (testcol BYTEA); 这项工作: ins

我有一个文本文件,其中有几个十六进制字符串:

013d7d16d7ad4fefb61bd95b765c8ceb
007687fc64b746569616414b78c81ef1
我想将这些存储在数据库中作为bytea,而不是varchar。也就是说,我希望数据库将01存储为单字节00000001,而不是字符“0”和“1”

我可以通过sed轻松运行此文件,以任何方式格式化/转义它

这就是我尝试过的:

create table mytable (testcol BYTEA);
这项工作:

insert into mytable (testcol) values (E'\x7f\x7f');
但是,一旦有一个字节超过\x7f,就会出现以下错误:

insert into mytable (testcol) values (E'\x7f\x80');
ERROR:  invalid byte sequence for encoding "UTF8": 0x80

有什么想法吗,或者我的方法有问题吗?

您可以使用
decode
函数将十六进制字符串转换为bytea(其中“编码”表示将二进制值编码为某些文本值)。例如:

select decode('DEADBEEF', 'hex');
      decode      
------------------
 \336\255\276\357
9.0的默认输出更容易理解:

   decode   
------------
 \xdeadbeef
不能只说
E'\xDE\xAD\xBE\xEF'
的原因是,这是为了生成文本值,而不是bytea,因此Postgresql将尝试将其从客户端编码转换为数据库编码。您可以这样编写bytea转义格式,但需要将反斜杠加倍:
E'\\336\\255\\276\\357'::bytea
。我想你可以明白为什么bytea格式正在改变。。。。IMHO
decode()
函数是写入输入的合理方式,尽管其中涉及一些开销

INSERT INTO mytable (testcol) VALUES (decode('013d7d16d7ad4fefb61bd95b765c8ceb', 'hex')) 插入 mytable(testcol) 价值观 (解码('013d7d16d7ad4fefb61bd95b765c8ceb','hex')) 红宝石之路 我最近需要通过Ruby从Postgres读/写二进制数据。下面是我如何使用

虽然不是严格的专为博士后准备的,但我想我应该包括这个以Ruby为中心的答案作为参考

Postgres数据库设置 插入二进制数据 选择二进制数据 以及
testcol
类型为
bytea
的杂项:

-- how to insert the string "123[a char of value zero]abc456"
insert into mytable (testcol) values decode(E'123\\000abc456', 'escape');

-- how to insert the string "123abc456"
insert into mytable (testcol) values decode(E'123abc456', 'escape');

-- how to insert in base64: insert string "abc456"
insert into mytable (testcol) values decode('YWJjNDU2', 'base64');
介绍 这是一个更新的答案,包括如何插入和如何查询

可以使用函数将十六进制转换为bytea值。这应该用于查询和插入

这既可以用于插入,也可以用于查询

查询现有数据 用于查询的编码与解码 一位用户询问了以下问题:

插入bytea字段后,如何按十六进制值搜索它

SELECT * FROM my_table WHERE myHexField =
(encode('013d7d16d7ad4fefb61bd95b765c8ceb', 'hex'));
不起作用

在文档中,它们都有
encode
decode
的描述

   +==================================+=============+=======================================================================================================+=======================================+============+
    |             Function             | Return Type |                                              Description                                              |                Example                |   Result   |
    +==================================+=============+=======================================================================================================+=======================================+============+
    | decode(string text, format text) | bytea       | Decode binary data from textual representation in string. Options for format are same as in encode.   | decode('123\000456', 'escape')        | 123\000456 |
    +----------------------------------+-------------+-------------------------------------------------------------------------------------------------------+---------------------------------------+------------+
    | encode(data bytea, format text)  | text        | Encode binary data into a textual representation. Supported formats are: base64, hex, escape. escape  | encode('123\000456'::bytea, 'escape') | 123\000456 |
    |                                  |             | converts zero bytes and high-bit-set bytes to octal sequences (\nnn) and doubles backslashes.         |                                       |            |
    +----------------------------------+-------------+-------------------------------------------------------------------------------------------------------+---------------------------------------+------------+
因此,您会注意到,
Encode
用于将二进制数据编码为文本字符串,并返回文本。然而,由于我们是通过tea存储

插入
From:请看

这个答案为OPT隐藏了太多的抽象,这拯救了我的一天。从testcol=(decode('013d7d16d7ad4fefb61bd95b765c8ceb','hex')的mytable中选择*;然后将匹配该
bytea
列。
-- how to insert the string "123[a char of value zero]abc456"
insert into mytable (testcol) values decode(E'123\\000abc456', 'escape');

-- how to insert the string "123abc456"
insert into mytable (testcol) values decode(E'123abc456', 'escape');

-- how to insert in base64: insert string "abc456"
insert into mytable (testcol) values decode('YWJjNDU2', 'base64');
SELECT * FROM mytable WHERE testcol = (decode('013d7d16d7ad4fefb61bd95b765c8ceb', 'hex'));
SELECT * FROM my_table WHERE myHexField =
(encode('013d7d16d7ad4fefb61bd95b765c8ceb', 'hex'));
   +==================================+=============+=======================================================================================================+=======================================+============+
    |             Function             | Return Type |                                              Description                                              |                Example                |   Result   |
    +==================================+=============+=======================================================================================================+=======================================+============+
    | decode(string text, format text) | bytea       | Decode binary data from textual representation in string. Options for format are same as in encode.   | decode('123\000456', 'escape')        | 123\000456 |
    +----------------------------------+-------------+-------------------------------------------------------------------------------------------------------+---------------------------------------+------------+
    | encode(data bytea, format text)  | text        | Encode binary data into a textual representation. Supported formats are: base64, hex, escape. escape  | encode('123\000456'::bytea, 'escape') | 123\000456 |
    |                                  |             | converts zero bytes and high-bit-set bytes to octal sequences (\nnn) and doubles backslashes.         |                                       |            |
    +----------------------------------+-------------+-------------------------------------------------------------------------------------------------------+---------------------------------------+------------+
create table mytable (testcol BYTEA);

INSERT INTO
  mytable (testcol)
VALUES
  (decode('013d7d16d7ad4fefb61bd95b765c8ceb', 'hex'));