Working with BLOBs

Creating Table with BLOB

CREATE TABLE MyData (
    ID            INTEGER NOT NULL,
    RECDATE       TIMESTAMP NOT NULL,
    PARAMVERSION  SMALLINT NOT NULL,
    PARAMDATA     BLOB SUB_TYPE 0 SEGMENT SIZE 2048
);

Blob subtypes definitions are: 0 - binary data (such as image, video, audio, etc.). 1 - text (standard text content). 2 - BLR (used for definitions of Firebird procedures, triggers, etc.).

User applications should only use subtype 0 or 1.

Writing BLOBs

//------------------------------------------------------------------------------
// description: Post data into BLOB from a user defined data structure.
// parameters : Data: TData; InstrumentID, ProdName: String; qry: TIBQuery
// return     : None
//------------------------------------------------------------------------------
procedure PostData(Data: TData; InstrumentID, ProdName: String; qry: TIBQuery);
var
  BlobStream: TStream;
  Buffer: array [0..2047] of Byte;
  pBuffer: Pointer;
  pData: Pointer;   
  i: integer;
begin
 // initialize variables
 for i:=0 to high(Buffer) do begin
    Buffer[i] := 0;
 end;
 
 try
    if not qry.IsEmpty then begin
       qry.Edit;    // edit the last entry for the day
    end else begin
       qry.Insert;  // add the entries if none available for the day
    end;
 
    // different methods to enter data into record
    if qry.State in [dsInsert] then begin
      qryPatientNo.Value        := '001';
      qryFittingDate.AsDateTime := Date();
      qryEar.Value              := 1;
      qryInstrumentID.Value     := InstrumentID;
    end;
    qry.FieldByName('InstrumentID').Value := 'INTU8';
    qry.FieldByName('Version').Value      := 100;    
 
    // store data into blob
    BlobStream := qry.CreateBlobStream(qry.FieldByName('Data'), bmWrite);  // create stream to read blob
    try
      pData   := @Data;
      pBuffer := @Buffer;
      move(pData^, pBuffer^, Sizeof(Data));       // move data to buffer array
      BlobStream.Write(Buffer[0], Sizeof(Data));  // write buffer array to blob in table
    finally
      BlobStream.Free;
    end;
    qry.Post;
 
 except
   on E:Exception do begin
     ShowMessage('PostData() raised exception ' + E.ClassName + ': '#13#10 + E.Message);
   end
 end;
end;

Reading BLOBs

//------------------------------------------------------------------------------
// description: Import data from BLOB into a user defined data structure.
// parameters : Data: TData; qry: TIBQuery
// return     : Boolean
//------------------------------------------------------------------------------
function ImportData(var Data: TData; qry: TIBQuery): Boolean;
var
  Buffer: array [0..2047] of byte;
  pBuffer: Pointer;
  pData: Pointer;
  BlobStream: TStream; // TBlobStream; does not work
  i: integer;
begin
  // initialize variables
  for i:=0 to high(Buffer) do begin
    Buffer[i] := 0;
  end;
 
  // read database blob and copy to user defined data structure
  pData    := @Data;
  pBuffer := @arrData;
  if not qry.IsEmpty then begin
    BlobStream:= qry.CreateBlobStream(qry.FieldByName('Data'), bmReadWrite);
    try
      BlobStream.Position := 0;
      BlobStream.Read(Buffer[0], SizeOf(Data));
      move(pBuffer^, pData^, SizeOf(Data));
    finally
      BlobStream.Free;
    end;
 
    result := True;
  end else begin
    result := False;
  end;
end;