INSERT AND READ IMAGE IN BLOB DATABASE

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
xerell
Posts: 8
Joined: Fri 02 Oct 2015 20:47

INSERT AND READ IMAGE IN BLOB DATABASE

Post by xerell » Fri 02 Oct 2015 20:50

Boa tarde, meu caso é o seguinte, já tentei de varias formas inserir uma imagem num campo blob (banco MYSQL) e depois ler, e não consigo.

O maximo que consegui foi salva-la, porém não sei se ficou no formato correto.

Preciso salvar imagem JPG no banco e depois recupera-la, qual a maneira mais simples de se fazer isso?

preciso de um exemplo

sorry for bad english

im using mydac 8.6

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: INSERT AND READ IMAGE IN BLOB DATABASE

Post by ViktorV » Mon 05 Oct 2015 10:56

You can use our MyDacDemo project. On the 'General Demos\Pictures' tab, there is demonstrated writing and reading graphic files to\from the database. This project can be found in the 'Demos\MyDacDemo' folder of the MyDAC demo projects installation directory.
Please write your question in English.

xerell
Posts: 8
Joined: Fri 02 Oct 2015 20:47

Re: INSERT AND READ IMAGE IN BLOB DATABASE

Post by xerell » Mon 05 Oct 2015 15:58

Good afternoon, my case is this , I've tried in many ways to insert a picture into a blob field (MYSQL database ) and then read , and I can not .

The maximum I got was save her , but do not know if it was the correct format.

I must save JPG image in the bank and then retrieve it , which the simplest way of doing this ?

I need an example

sorry for bad Inglês

im using MyDAC 8.6

( I do not know why the question appeared in Portuguese , I translated correctly)

xerell
Posts: 8
Joined: Fri 02 Oct 2015 20:47

Re: INSERT AND READ IMAGE IN BLOB DATABASE

Post by xerell » Mon 05 Oct 2015 17:53

Hello , my demo directory of the picture seems to be in trouble, the Delphi does not open the form so I can not see how the operation .

xerell
Posts: 8
Joined: Fri 02 Oct 2015 20:47

Re: INSERT AND READ IMAGE IN BLOB DATABASE

Post by xerell » Mon 05 Oct 2015 18:30

Well, come on , I managed to open the project here understand, but do not apply too much. You see, I am developing for mobile, I carry a picture in a TImage (either the library or direct the camera) , this photo loaded in Timagem I need to go to the database, can not use DIALOGS , only the picture loaded . below is my insert.



qenvia_ocorrencia := TMyQuery.Create(nil);
qenvia_ocorrencia.Connection := datamodule3.MyConnection1;
qenvia_ocorrencia.SQL.Clear;
qenvia_ocorrencia.SQL.Add('INSERT INTO SOLICITACOES (protocolo, status, data, hora, ocorrencia, rua, numero, bairro, cep, link_maps,obs,movimentacao,imagem, cod_usuario)');
qenvia_ocorrencia.SQL.Add('VALUES (:protocolo, :status, :data, :hora, :ocorrencia, :rua, :numero, :bairro, :cep, :link_maps,:obs,:movimentacao, :imagem, :cod_usuario)');
qenvia_ocorrencia.ParamByName('PROTOCOLO').AsString := '10'; //pegar o ultimo protocolo
qenvia_ocorrencia.ParamByName('STATUS').AsString := 'TESTE';
qenvia_ocorrencia.ParamByName('DATA').AsDate := DATE;
qenvia_ocorrencia.ParamByName('HORA').AsDateTime := TIME;
qenvia_ocorrencia.ParamByName('OCORRENCIA').AsString := texto_listbox;
qenvia_ocorrencia.ParamByName('RUA').AsString := edtRua.Text;
qenvia_ocorrencia.ParamByName('NUMERO').AsInteger := StrToInt(edtNumero.Text);
qenvia_ocorrencia.ParamByName('BAIRRO').AsString := edtBairro.Text;
qenvia_ocorrencia.ParamByName('CEP').AsString := edtCep.Text;
qenvia_ocorrencia.ParamByName('LINK_MAPS').AsString := edtUrl.Text;
qenvia_ocorrencia.ParamByName('OBS').AsString := memDetalhes.Text;
qenvia_ocorrencia.ParamByName('MOVIMENTACAO').AsString := 'TRAMITADO';
qenvia_ocorrencia.ParamByName('IMAGEM').LoadFromStream(imgfoto ftBlob);
qenvia_ocorrencia.ParamByName('COD_USUARIO').AsInteger := ufrmInicial.codigo_usuario_global;
qenvia_ocorrencia.Execute;

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: INSERT AND READ IMAGE IN BLOB DATABASE

Post by ViktorV » Tue 06 Oct 2015 11:36

To load graphic files like JPEG in a Blob field, you can use the following code:

Code: Select all

procedure TForm1.Button1Click(Sender: TObject);
var
  BlobStream: tStream;
  Surf: TBitmapSurface;
begin
  if Image1 <> nil then
  begin
    Surf := TBitmapSurface.Create;
    Surf.Assign(Image1.Bitmap);
    BlobStream := TMemoryStream.Create;
    if not TBitmapCodecManager.SaveToStream(BlobStream, Surf, '.jpg') then
      raise EBitmapSavingFailed.Create('No');
    BlobStream.Position := 0;
    myquery2.ParamByName('imagem').LoadFromStream(BlobStream, ftBlob);
    myquery2.ExecSQL;
    Surf.Free;
  end;
end;
To load data from the Blob field to the TImage component, use the following code:

Code: Select all

procedure TForm1.Button2Click(Sender: TObject);
var
  BlobStream: TStream;
begin
  BlobStream := TMemoryStream.Create;
  myquery1.GetBlob(myQuery1.FieldByName('imagem')).SaveToStream(BlobStream);
  Blobstream.Position := 0;
  Image2.bitmap.loadfromstream(blobstream);
  BlobStream.Free;
end;

xerell
Posts: 8
Joined: Fri 02 Oct 2015 20:47

Re: INSERT AND READ IMAGE IN BLOB DATABASE

Post by xerell » Tue 06 Oct 2015 18:28

Friend, grateful for the response, I think we're on the road , but only look at the picture, there is no such TBitmapSurface or would have to use some special Unit ? I will replace TBitmapSurface by TBitmap but not saved the photo on the bench.

Image

xerell
Posts: 8
Joined: Fri 02 Oct 2015 20:47

Re: INSERT AND READ IMAGE IN BLOB DATABASE

Post by xerell » Tue 06 Oct 2015 19:36

I think the problem was to declare that library : FMX.Surfaces

I think I could save the image , because the bank already has a string in the field ..

In TImage I used the " picture" before the bitmap is a bitmap for picute class " Image1.Picture.Bitmap.loadfromstream ( blobstream ) ; "
To compile and run the function of the following error:

Bitmap image is not valid .

Dacuser
Posts: 2
Joined: Mon 13 Jul 2015 17:39

Re: INSERT AND READ IMAGE IN BLOB DATABASE

Post by Dacuser » Tue 06 Oct 2015 19:57

.. In the VCL you would do
Image1.Picture.Bitmap.LoadFromStream(lStream);
In FMX you do
Image1.Bitmap.LoadFromStream(lStream);
Last edited by Dacuser on Tue 06 Oct 2015 20:45, edited 1 time in total.

xerell
Posts: 8
Joined: Fri 02 Oct 2015 20:47

Re: INSERT AND READ IMAGE IN BLOB DATABASE

Post by xerell » Tue 06 Oct 2015 20:27

Hello friend, to burn the image I am using a client for android and IOS , the recording seems to be working .
below follows the string written to the blob :
Image

I'm trying to read in VCL windows32 and appears the image below:
Image

Codigo:

Code: Select all

procedure TForm2.Button1Click(Sender: TObject);
var
BlobStream: TStream;
begin
   myquery1.Close;
   MyQuery1.Open;
   BlobStream := TMemoryStream.Create;
   myquery1.GetBlob(myQuery1.FieldByName('IMAGEM')).SaveToStream(BlobStream);
   Blobstream.Position := 0;
   Image1.Picture.Bitmap.LoadFromStream(blobstream);
   BlobStream.Free;

I'm using Delphi XE 10 Seattle

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: INSERT AND READ IMAGE IN BLOB DATABASE

Post by ViktorV » Wed 07 Oct 2015 12:29

To load data from the Blob field to the VCL TImage component, use the following code:

Code: Select all

procedure TForm1.Button2Click(Sender: TObject);
var
  BlobStream: TStream;
  Jpg: TJPEGImage;
begin
  BlobStream := TMemoryStream.Create;
  myquery1.GetBlob(myQuery1.FieldByName('imagem')).SaveToStream(BlobStream);
  Blobstream.Position := 0;
  Jpg := TJPEGImage.Create;
  try
    Jpg.LoadFromStream(BlobStream);
    Image2.Picture.Assign(Jpg);
  finally
    Jpg.Free;
  end;
  BlobStream.Free;
end;
Add the Jpeg mode to the Uses section to use this code.

xerell
Posts: 8
Joined: Fri 02 Oct 2015 20:47

Re: INSERT AND READ IMAGE IN BLOB DATABASE

Post by xerell » Wed 07 Oct 2015 16:57

Hello thank you so much! I can already save and retrieve photos . thank attention.

ps: the first code to retrieve photos that you wrote is for mobile?

:D :D

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: INSERT AND READ IMAGE IN BLOB DATABASE

Post by ViktorV » Thu 08 Oct 2015 10:44

It is good to see that the problem has been solved. Feel free to contact us if you have any further questions about MyDAC.
Yes, you are right. The first code sample is designed for mobile development.

Post Reply