TDump image

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
davor.TCS
Posts: 29
Joined: Thu 05 Apr 2012 22:10
Contact:

TDump image

Post by davor.TCS » Thu 03 Jun 2021 17:50

In my DB image is saved to a blob field. If I export an image and then import it, sometimes I get wrong data and the image is not shown or an exception occurs. What is the best way to export image to *.sql file?
If I use HexBlob option in TDump, all is ok, but I have fields in DB that are blob fields and contain text that I don't want to export as hex.
Is it possible to do:
If image then
DoHex
else
DoRegular
regarding a column data?
Or perhaps to use TEXT field type for data that are strings?

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

Re: TDump image

Post by ViktorV » Fri 11 Jun 2021 05:33

Hi there,

Regarding incorrect import/restore of image into blob field- please compose a sample which demonstrates described behavior and contains scripts to create and fill-in database objects along with import files and send it to us using contact form:
https://www.devart.com/company/contactform.html
As for enabled option HexBlob in TMyDump - import/restore should be ok. Please note that when HexBlob option is enabled, into hex codding only Blobs and string fields with BINARY attribute will be saved.

Best regards,
Viktor

davor.TCS
Posts: 29
Joined: Thu 05 Apr 2012 22:10
Contact:

Re: TDump image

Post by davor.TCS » Fri 25 Jun 2021 15:41

I use next procedure for import (*.sql file is compressed)

Code: Select all

procedure ImportDB;
var
MyZipFile: TZipFile;
DataStream: TMemoryStream;
MyByteData: TBytes;
begin
  if OpenDlg.Execute then
    begin
      DataStream:=TMemoryStream.Create;
      MyZipFile:=TZipFile.Create;
      try
        if MyZipFile.IsValid(OpenDlg.FileName) then
          begin
            MyZipFile.Open(OpenDlg.FileName, zmRead);
            if (MyZipFile.FileCount = 1) then
              begin
                MyZipFile.Read(0, MyByteData);
                DataStream.WriteBuffer(MyByteData[0], Length(MyByteData));
                //DM.QryDump.Options.HexBlob:=True;
                DM.QryDump.RestoreFromStream(DataStream);
              end;
            MyZipFile.Close;
          end;
      finally
        DataStream.Free;
        MyZipFile.Free;
      end;
    end;
end;
Field to store image is BLOB type field (mediumblob). Perhaps it is not possible to import it like this in TMemoryStream? Perhaps TStringStream with UTF8 is more appropriate?

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

Re: TDump image

Post by ViktorV » Tue 29 Jun 2021 14:49

Hi there,

You can use that code for the task. Please note that the HexBlob option affects only the backup operation. Please create the SQL script (from the archive) using TMyDump with HexBlob enabled and try importing data again.
I did a simple test: backed up several records with images from the MediumBlob field and with HexBlob enabled, added the resulting SQL script to the archive, and then successfully restored data from the archive using your code.
If this doesn't help, please create a full sample project demonstrating the issue and send it to me along with the scripts for creating and populating database objects, and the image files, using the form: https://www.devart.com/company/contactform.html

Best regards,
Viktor

davor.TCS
Posts: 29
Joined: Thu 05 Apr 2012 22:10
Contact:

Re: TDump image

Post by davor.TCS » Tue 13 Jul 2021 06:13

Curently I have enabled HexBlob, fields with text altered to be mediumtext (fields for images retained as blob definitions), and all works fine. Will need some time to test this.

What if I import with TDump file that has blob data in Hex? Do I need to have DM.QryDump.Options.HexBlob:=True; or TDump checks this automatically?

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

Re: TDump image

Post by ViktorV » Wed 14 Jul 2021 15:03

Hi there!

As we have already mentioned in the previous forum topic, the HexBlob option affects only the Backup operation and defines in what form the BLOB will be written to the resulting dump file.
Please note that during the Restore operation this option is not used.

Regards,
Viktor

Post Reply