Save image in the POSTGRES database with DELPHI Tokyo by the Android App

1

I need to save a photo that is taken to the tablet's camera and save it to the database. Type the field the 'typea'. I'm using the Timage component to view and reference the image, but I can not save it in the database.

Thank you.

    
asked by anonymous 29.09.2017 / 23:48

1 answer

0

You can use the pg_read_binary_file() function to read binary files stored in data_directory of the postgres server.

Consider the table:

CREATE TABLE tb_imagem
(
    id BIGINT,
    nome TEXT,
    imagem bytea
);

Images can be read from the server disk and immediately inserted into the database in a field of type bytea :

INSERT INTO tb_imagem ( id, nome, imagem ) VALUES ( 1, 'foobar', pg_read_binary_file( '/var/lib/pgsql/data/imagem1.jpg' ) );
INSERT INTO tb_imagem ( id, nome, imagem ) VALUES ( 2, 'xpto', pg_read_binary_file( '/var/lib/pgsql/data/imagem2.jpg' ) );
INSERT INTO tb_imagem ( id, nome, imagem ) VALUES ( 3, 'kwy', pg_read_binary_file( '/var/lib/pgsql/data/imagem3.jpg' ) );

Then you can retrieve the image as base64 through the function encode() :

SELECT encode( imagem,'base64') FROM tb_imagem WHERE id = 1; 
    
30.09.2017 / 02:08