1

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

Thanks

jachguate
  • 16,976
  • 3
  • 57
  • 98
user299323
  • 55
  • 2
  • 8

3 Answers3

1

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;
Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • I don't use ABSTable but rather ABSQuery :) This will not work – user299323 Mar 14 '13 at 16:34
  • @RobKennedy: You can get rid of the type-cast by declaring `BlobStream` as a generic `TStream` instead of a `TABSBlobStream` specifically. `CreateBlobStream()` returns a generic `TStream` pointer, an this code is not using any `TABSBlobStream`-specific functionality. – Remy Lebeau Mar 14 '13 at 20:34
  • @user2153148: try `Field.DataSet.CreateBlobStream(Field, ...)`;` – Remy Lebeau Mar 14 '13 at 20:37
0

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

RBA
  • 12,337
  • 16
  • 79
  • 126
  • And where is TACRBlobStream defined? – user299323 Mar 14 '13 at 15:31
  • It's in the [`ACRMain`](http://www.aidaim.com/products/acr/ref_cross-platform_client-server_single-file_embedded_bde_replacement_database_sql_delphi_c++builder_kylix/tsqlmemblobstream.php) unit. – TLama Mar 14 '13 at 15:33
0

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

Sir Rufo
  • 18,395
  • 2
  • 39
  • 73