how to insert image into the database

2

What kind of variable do I use to store an image in the database? And is there a specific command for this? or just insert as any record?

I have a project in the language lua (mobile by corona sdk) that the person has to take a photo and I need to save this photo in the PostgreSQL database.

I have a publication table with the fields:

code, location, time, description, category, image in the image I intend to store a photo taken by the user.

Knowing that the mobile phone will be sent to the web service that will add in the bank and another type of user will be able to have access to these publications only over the web. How can I insert it? Can someone help me?

    
asked by anonymous 24.05.2017 / 19:48

4 answers

0

Use the BYTEA type which is an array of bytes. As for the Lua language, I do not know how to tell you, but in C #, just pass byte[] as a parameter normally.

Tip: Prefer to use separate table of images from the records table, so you can insert multiple images into the same record.

Example in C # + ODBC:

byte[] Imagem = (arquivo de imagem em bytes);

OdbcCommand cmd = new OdbcCommand(/*string de conexão*/);
cmd.CommandText = @"INSERT INTO [Tabela]
(coluna_bytea) VALUES (?);";

OdbcParameter[] paramC = new OdbcParameter[1];
paramC[0] = new OdbcParameter("coluna_bytea", OdbcType.Binary);
paramC[0].Value = ((Imagem == null || Imagem.Length == 0 )? null : Imagem);

cmd.ExecuteNonQuery();
    
24.05.2017 / 19:54
-1

To store the photo, who does this is the language of BackEnd, eg PHP. To send ...

http = require("socket.http")

ltn12 = require ("ltn12")

http.request{
    url = "SEU-SERVIDOR/ARQUIVO.PHP",
    method = "POST",
    headers = {
        ["Content-Type"] =  "multipart/form-data",
        ["Content-Length"] = sizeOfFile
    },
    source = ltn12.source.file(io.open(pathToLocalFile)),
    sink = ltn12.sink.table(response_body)
}
print(response_body[1])
    
24.05.2017 / 19:54
-1

I can help you in creating the mySql table. Take a look at the code below,

  create table imagens (
        imagem_id        tinyint(3)  not null default '0',
        imagem_type      varchar(25) not null default ' ',
        image            LONGBLOB    not null,
        imagem_size      varchar(25) not null default ' ',
        imagem_catg      varchar(25) not null default ' ',
        imagem_name      varchar(50) not null default ' ',
    );
    
24.05.2017 / 20:04
-1

According to PostgreSQL documentation

You can use the bytea format to write binaries:

CREATE TABLE images (imgname text, img bytea);

Creating Lua table

require"luasql.postgres"                       -- carrega a biblioteca postgres
env = assert (luasql.postgres())               -- cria a variável de ambiente
con = assert (env:connect("luasql-test"))      -- conectando ao banco
res = assert (con:execute[[ CREATE TABLE images (imgname text, img bytea); ]])     -- criando tabela binária

Saving data binaries:

res = assert (con:execute[[ INSERT INTO images values ('imagem.jpg', pg_read_file('/caminho/da/imagem.jpg')::bytea) ); ]])
    
17.07.2017 / 21:07