Lazarus: saving BLOBs from Sybase database

Hi there,

I am trying to save files which are stored in BLOB fields in a Sybase database.

The routine below seems to work, files are being saved in my folder, but when opening them they seem to be corrupt. For example when I open a Word or Excel file it needs to be repaired (by Office) before I can read the contents.

procedure TForm1.saveBlobs;
var tmpFilename: String;
    tmpList: TStringList;
begin
  Query1.First;
  While not Query1.Eof do begin
    tmpFilename := prgPath + '_output\' + Query1.FieldByName('DOCU_ORG_FILENAME').AsString;

    tmpList := TStringList.Create;
    tmpList.Append(Query1.FieldByName('DOCU_DOCUMENT').AsVariant);
    tmpList.SaveToFile(tmpFilename);
    tmpList.Free;

    Query1.Next;
  end;
end;

Open in new window

Could my routine be the root cause of these corruptions?

Could you please help me out? Notice: I am using Lazarus (@Windows), not all Delphi stuff is available.

Kind Regards,
Stefan
Stefan van RoosmalenfounderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jimyXCommented:
Hi Stefan,
I would have avoided the StringList, and used streams:

procedure TForm1.saveBlobs;
var tmpFilename: String;
    //tmpList: TStringList;
  BlobStream: TStream;
  ms: TMemoryStream;
begin
  Query1.First;
  While not Query1.Eof do begin
    tmpFilename := prgPath + '_output\' + Query1.FieldByName('DOCU_ORG_FILENAME').AsString;

    //tmpList := TStringList.Create;
    //tmpList.Append(Query1.FieldByName('DOCU_DOCUMENT').AsVariant);
    //tmpList.SaveToFile(tmpFilename);
    //tmpList.Free;

    BlobStream := CreateBlobStream(Query1.FieldByName('DOCU_DOCUMENT'), bmRead);
    try
      ms:= TMemoryStream.Create;
      try
        ms.LoadFromStream(BlobStream);
        ms.SaveToFile(tmpFilename);
      finally
        ms.Free;
      end;
    finally
      BlobStream.Free;
    end;

    Query1.Next;
  end;
end;

Open in new window


Could you inspect the extracted file by Notepad++ to see if there are any unusual additions to the extracted data? i.e compare repaired file against same file but before doing repair. (make sure to pick any file that has got reasonable size to find differences quicker).

After this you can tell whether it was the routine, or the originally stored file is already stored corrupted.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Stefan van RoosmalenfounderAuthor Commented:
I have tried this but then I run into this error during compiling:
Error: Identifier not found "CreateBlobStream"
But I have found the trick:
BlobStream := Query1.CreateBlobStream(Query1.FieldByName('DOCU_DOCUMENT'), bmRead);

Open in new window

Stefan van RoosmalenfounderAuthor Commented:
You have helped me with the right direction... thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.