how to insert picture into database

Asked

Viewed 5,462 times

2

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

i have a project in the moon language ( 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 publishing 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 will be sent to the web service that will add in the bank and other type of user will be able to access these publications only through the web. how can I insert? someone can help me?

  • My 'whole life' I listened to never do that (save image in the database). But that was a long time ago, when the internet connection was very limited. Although it has the issue of scalability as well. I can’t tell if this is good because I don’t know the scope of your project. There are some answers below. The one that best suit, mark as right, and if possible speaks only a little more of your project.

  • solved your problem ?

  • So Paul ( sorry if I didn’t mark you, I’m new here and I don’t know if it has to mark) but so, my project has 3 user table, organ and publication. let’s focus on the publication ( I will separate in two comments because I exceeded the character limit )

  • a user will take a photo and put a description for it, a category and a location also ( the time the system picks up automatically ) . from the moment it publishes it goes to the web service that will save in the bank and to anyone else by browser can have access to these publications . and like instagram, got it? only simpler ( since I’m learning a new language ... so I’m doing the basics. ps: if there is an accent error these sorry things I am typing from a keyboard of type Eng

4 answers

1


Use the BYTEA type which is an array of bytes. As for the language Lua, I can’t speak to you, but in C#, just pass the byte[] as a normal parameter.

Tip: Prefer to use separate picture table from the record table, so you can insert multiple images for 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();
  • good idea! thanks for the tip ! ja with respect to the type if I put a column of this type and pass an image it already saved?

  • As I told you in the answer, I don’t know how it does it in the moon language... but the field is of type array of bytes, can save any file in it, not only images, exe, dll, doc, xls, pdf. Then you have to send a parameter of the same type to the database (array of bytes)

  • incomplete and indirect answer, outside the question tag.

  • @Rafaellemos so much that helped the member and was marked as a response, there you appear, mark all others as negative and put yours. Congratulations

  • @Ronann Linhalis All right, no quarrels.

-1

To store the photo, who does this is the language of Backend, ex: 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])
  • incomplete and indirect answer, outside the question tag.

-1

I can help you in the part of 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 ' ',
    );
  • ok friend, but like this. the image in this case will stay in the image column of the longblob type?

  • incomplete and indirect answer, outside the question tag.

-1

According to the Postgresql documentation

You can use the format bytea to record binaries:

CREATE TABLE images (imgname text, img bytea);

Creating table in Lua:

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

Recording data binaries:

res = assert (con:execute[[ INSERT INTO images values ('imagem.jpg', pg_read_file('/caminho/da/imagem.jpg')::bytea) ); ]])
  • Moderator, tell me what is wrong with this answer.

Browser other questions tagged

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