How to Insert/Recover Image in Mysql Database

Asked

Viewed 5,018 times

6

I would like to know in a clear and simple way how to insert/recover image in Mysql database?

I’m using Firedac to connect to the Mysql database.

I have a JPEG image and want to save it in a Mysql database and be able to restore it when necessary. From what I can see it’s something of a data type blob, but I don’t know the right way to do it.

  • When you say, "insert/recover database image" you mean create/restore ?

  • Yes, Exactly, Insert Image, and Query/Restore BD Image.

  • Viniam, This video shows the whole process, from the capture by the Image component to the passage of parameter to STP, in Delphi 7, but it is for SQL Server, however, can give a light! Follow the video link: https://youtu.be/8pGGDFG_Kis

1 answer

5


After a connection made with the FireDac, you can use one of your own components to execute the commands SQLs.

The Component is in the same tab of FireDacconnection, this is about the FDQuery.

Saving the image in the Database:

procedure frmTeste.btnIncluirFotoClick(Sender: TObject);
var
  vArquivo: TFileStream;
begin
  vArquivo := TFileStream.Create('d:\01.bmp', fmOpenRead or fmShareDenyWrite);
  FDQuery1.Close;
  FDQuery1.SQL.Clear;
  //Pode ser feito Insert ou Update
  FDQuery1.SQL.Add('INSERT INTO tbl_usuario (FOTO) VALUES(:FOTOCARREGADA)');
  FDQuery1.Params[0].DataType := ftBlob;
  FDQuery1.ParamByName('FOTOCARREGADA').AsStream := vArquivo;
  FDQuery1.ExecSQL;
  FDQuery1.Close;
end;

Reading the Database Image:

procedure frmTeste.btnCarregarFotoClick(Sender: TObject);
var
  vFoto   : TStream;
  vImagem : TBitmap;
begin     
  FDQuery1.Close;
  FDQuery1.SQL.Add('SELECT * FROM tbl_usuario WHERE id_usuario = 6');
  FDQuery1.Open;

  //Lendo a imagem do campo BLOB para a Memória
  vFoto := FDQuery1.CreateBlobStream(FDQuery1.FieldByName('FOTO'), bmRead);

  //Criando a instância de TBitmap (pode ser JPG tb)
  vImagem := TBitmap.Create;

  //Carregando a imagem a partir do stream TStream
  vImagem.LoadFromStream(vFoto);

  //Exibindo a Imagem
  Image1.Picture.Assign(vImagem);
  //Image1.Bitmap.LoadFromStream(vFoto); //Caso esteja usando FMX ao invés de VCL
end;

To have a success in writing and reading the data, choose the field type Blob must be taken into account!

Follow a question I asked right here with good answers about the field BLOB

  • No friend, I meant insert image to mysql database, because I know how to make use of firedac, insert and good queries, My question is the insertion of Images in the database, Insert and bring the image back, Understood? Unfortunately I have already searched in several languages and so far not seen something that works properly.

  • Even though you explained in this comment I didn’t understand anything yet, I replied thinking it was something related to this, come on, Picture, are you referring to Bitmap? JPEG ?

  • Well I refer to JPEG image, I will try to be clearer this time: Good I have a JPEG image and I want to store it in a mysql database and be able to restore it when necessary. From what I could see is something kind of data blob, but I don’t know the right way to do it, understood?

  • I understand, rephrasing the Answer, please add this information to the question!

  • I already rephrased the question :).

  • @Viniam we go to Testes, edited response!

  • Junior Moreira, Thank you very much for the help man, so I tested here and managed to insert the image to the database worked perfectly, but unfortunately I am not able to do the recovery of the image(query) ... Note we are in Delphi xe8 Multi-device: Image1.Picture.Assign(vImage); , It does not exist, so I changed to: Image1.MultiResBitmap.Assign(vImage); ... You’re making a mistake when you consult the comic :(

  • @Viniam test this: Image1.Bitmap.LoadFromStream(vFoto);

  • It worked perfectly, I’ve rated the answer as Solved, Congratulations and thanks for the help.

Show 4 more comments

Browser other questions tagged

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