ReadWrite TMemoryStream from BLOB

6.4k Views Asked by At

How can I store a TMemoryStream to a BLOBB Field and read from it using Accuracer DB / SQL. With SQL I mean ABSQuery..

Thanks

3

There are 3 best solutions below

0
On BEST ANSWER

Your question is not very clear to me (and looking at the answers and your comments it's the same to all other). So this is just a shot in the dark ...

Absolute Database has a conversion function MimeToBin to convert a MIME-encoded (string) value to a binary data type.

Sample for MimeToBin (The sample Data is just Hello World)

INSERT INTO table ( BinData )
VALUES ( MimeToBin( 'SGVsbG8gV29ybGQ=' ) )

To get such a Base64 encoded string from a stream use ABSDecUtil.TStringFormat_MIME64.

uses
  ABSDecUtil; 

function BuildSQLFromStream( AStream : TMemoryStream ) : string;
var
  LCoder : TStringFormat_MIME64;
begin
  LCoder := TStringFormat_MIME64.Create;
  try
    RESULT := 
      'INSERT INTO table ( BinData ) ' +
      'VALUES( MimeToBin( ' + 
      QuotedStr( 
        LCoder.StrTo( 
          AStream.Memory, 
          AStream.Size ) ) + 
      ' ) )';
  finally
    LCoder.Free;
  end;
end;

As this is all just a little abstract, you can get a sample project from Component Ace

2
On

The following example reads the data from a memo field into a blob stream and displays it in a memo control.

procedure TForm1.Button2Click(Sender: TObject);
var
Buffer: PChar;
MemSize: Integer;
Stream: TACRBlobStream;
begin
Stream := TACRBlobStream.Create(MyAccuracer.FieldByName('Notes') as TBlobField, bmRead);
try
MemSize := Stream.Size;
Inc(MemSize); //Make room for the buffer's null terminator.
Buffer := AllocMem(MemSize); //Allocate the memory.
try
Stream.Read(Buffer^, MemSize); //Read Notes field into buffer.
Memo1.SetTextBuf(Buffer);// Display the buffer's contents.
finally
FreeMem(Buffer, MemSize);
end;
finally
Stream.Free;
end;
end; 

from here:

http://www.aidaim.com/products/acr/guide_bde_alternative_client-server_single-file_embedded_bde_replacement_database_delphi_c++builder_kylix/blob_fields_use.php

3
On

The documentation for BLOB fields shows you how. The code there demonstrates storing a file into a BLOB field, but you can adapt it to store the contents of any kind stream, not just TFileStream. The key is to create a BLOB stream, and then call CopyFrom, which copies the contents of one stream into another.

ABSTable1.Edit;
try
  BlobStream := ABSTable1.CreateBlobStream(Field, bmWrite) as TABSBlobStream;
  try
    BlobStream.CopyFrom(Stream, 0);
  finally
    BlobStream.Free;
  end;
  ABSTable1.Post;
except
  ABSTable1.Cancel;
  raise;
end;