Recent

Author Topic: BLOB and MySQL  (Read 15535 times)

IndianaJones

  • Hero Member
  • *****
  • Posts: 509
BLOB and MySQL
« on: November 09, 2009, 07:16:36 am »

Hi all,

I am using zeos components and MySQL. I just want to learn how can i add/insert a jpeg image to the MySQL database?
Thanks.

IndianaJones

  • Hero Member
  • *****
  • Posts: 509
Re: BLOB and MySQL
« Reply #1 on: November 10, 2009, 07:01:15 pm »

Hi all,

I found this example on the net, and works fine. Following code save the jpeg stream data to the database. In my example 'simaj' is the BLOB field in the mysql table.
But I have a problem. I saved the picture to the database but reading jpeg data is not working. Second code is reading data from blob field to the Image component, but I cant figure it out.
Anyone has idea?
Thanks.

//------------------------------Load Picture to the BLOB data-------------------------
procedure TForm1.Button3Click(Sender: TObject);
var
  Jpg: TJpegImage;
  Stream: TMemoryStream;
  FileExt: string;
  GraphType: TGraphType;
begin

if dlgOpenPicture.Execute then
begin
    Jpg := nil;
    Stream := nil;
try
    Stream := TMemoryStream.Create;
    FileExt := LowerCase(ExtractFileExt(dlgOpenPicture.FileName));
  if (FileExt = '.jpg') or (FileExt = '.jpeg') or (FileExt = '.jpe') then
    begin
      Jpg := TJpegImage.Create;
      Jpg.LoadFromFile(dlgOpenPicture.FileName);
      Image1.Picture.Assign(Jpg);
      GraphType := gtJpeg;
      Stream.Write(GraphType, 1);
      Jpg.SaveToStream(Stream);
    end;

if (ZTable1.State <> dsEdit) and (ZTable1.State <> dsInsert) then ZTable1.Edit;
    Stream.Position := 0;
    TBlobField(ZTable1.FieldByName('simaj')).LoadFromStream(Stream);

except
    jpg.Free;
    Stream.Free;
raise;
end;
    jpg.Free;
    Stream.Free;
end;
end;
//------------------------------Load Picture to the BLOB data-------------------------

//---------------------------------Reading Stream data to the Image component-----------------------------
procedure TForm1.Image1Click(Sender: TObject);
var
    Stream: TMemoryStream;
    Jpg: TJpegImage;
begin
    Jpg := nil;
    Stream := nil;
    try
      // Create a stream and load the contents of the Blob field
      Stream := TMemoryStream.Create;
      TBlobField(ZTable1.FieldByName('simaj')).SaveToStream(Stream);
      if Stream.Size > 0 then begin
        // Create a JPEG image and load it from the stream
        Jpg := TJpegImage.Create;
        Stream.Position := 0;
        Jpg.LoadFromStream(Stream);
        // Assign the JEPG image to the Picture property of an Image
        Image1.Picture.Assign(Jpg);
      end
      else
        Image1.Picture.Assign(nil);
    except
      Image1.Picture.Assign(nil);
    end;
    jpg.Free;
    Stream.Free;
end;
//---------------------------------Reading Stream data to the Image component-----------------------------


jonas shinaniganz

  • New member
  • *
  • Posts: 8
Re: BLOB and MySQL
« Reply #2 on: November 18, 2011, 11:01:16 am »
Hi its been a while but for now future references, your secound code also works. Just try to read the data in, not from the datafield of ur grid but from the query you are using itself. therefore try:

//---------------------------------Reading Stream data to the Image component-----------------------------
procedure TForm1.Image1Click(Sender: TObject);
var
    Stream: TMemoryStream;
    Jpg: TJpegImage;
begin
    Jpg := nil;
    Stream := nil;
    try
      // Create a stream and load the contents of the Blob field
      Stream := TMemoryStream.Create;


  // Instead of:    TBlobField(ZTable1.FieldByName('simaj')).SaveToStream(Stream);
 // do this:
         // SQLQuery1 is the name of the query i used in my test, put whatever query you use ofc ^^
         TBlobField(SQLQuery1.FieldByName('simaj')).SaveToStream(Stream);

      if Stream.Size > 0 then begin
        // Create a JPEG image and load it from the stream
        Jpg := TJpegImage.Create;
        Stream.Position := 0;
        Jpg.LoadFromStream(Stream);
        // Assign the JEPG image to the Picture property of an Image
        Image1.Picture.Assign(Jpg);
      end
      else
        Image1.Picture.Assign(nil);
    except
      Image1.Picture.Assign(nil);
    end;
    jpg.Free;
    Stream.Free;
end;
//---------------------------------Reading Stream data to the Image component-----------------------------

 

TinyPortal © 2005-2018