Inserting text string with hex into PostgreSQL as a bytea

前端 未结 5 2013
抹茶落季
抹茶落季 2020-12-13 05:50

I have a text file with several strings of hex in it:

013d7d16d7ad4fefb61bd95b765c8ceb
007687fc64b746569616414b78c81ef1

I would like to sto

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-13 06:18

    Introduction

    This is an updated answer that includes both how to insert but also how to query.

    It is possible to convert the hex into a bytea value using the decode function. This should be used for both querying and also inserting.

    This can be used for both inserting but also querying.

    Example SQL Fiddle

    Querying Existing Data

    SELECT * FROM mytable WHERE testcol = (decode('013d7d16d7ad4fefb61bd95b765c8ceb', 'hex'));
    

    Encode vs Decode for Querying

    A user had asked the following:

    How does searching the bytea field by hex value after inserting it?

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

    does not work.

    In the documentation Binary String Functions and Operators, they have the description of both encode and 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.         |                                       |            |
        +----------------------------------+-------------+-------------------------------------------------------------------------------------------------------+---------------------------------------+------------+
    

    So you will notice that Encode is for encoding binary data into a textual string and returns text. However, since we are storing bytea we have to use decode for both inserting and querying.

    Inserting

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

    From: see previous answer

提交回复
热议问题