Save image to POSTGRES database with DELPHI Tokyo by Android App

Asked

Viewed 1,171 times

2

I need to save a photo that is taken on the tablet camera and save it in the database. The field type is typea. I’m using the Timage component to view and reference the image, but I can’t save it to the database.

Grateful.

1 answer

1

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

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' ) );

After, you can recover the image in representation base64 through the function encode() :

SELECT encode( imagem,'base64') FROM tb_imagem WHERE id = 1; 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.