Delphi Examples: DbiTruncateBlob

<< Click to Display Table of Contents >>

Navigation:  »No topics above this level«

Delphi Examples: DbiTruncateBlob

Return to chapter overview

Truncate all BLOBs in the specified field to zero. If any error occurs while removing BLOB information, stop at that record.

This example uses the following input:

 fDbiTruncateBlob(BiotestTbl, BiotestTbl.FieldByName('Notes').Index);

 

The procedure is:

procedure fDbiTruncateBlob(BlobTbl: TTable; Index: Word);

var

 hCur: hDBICur;

 pRecBuf: pBYTE;

begin

 hCur := nil;

 // Make sure the field specified is a BLOb type

if (BlobTbl.Fields[Index] is TblobField) then begin

   pRecBuf := AllocMem(BlobTbl.RecordSize);

  try

     // Clone a cursor to the table so data aware controls keep their place

     Check(DbiCloneCursor(BlobTbl.Handle, False, False, hCur));

     Check(DbiSetToBegin(hCur));

     // Iterate throuth the table removing BLOb information

    while (DbiGetNextRecord(hCur, dbiWRITELOCK, pRecBuf, nil) = DBIERR_NONE)

    do begin

       // BDE funcstions use a 1 for the first field vs. Delphi's 0;

       // add 1 to the index

       Check(DbiOpenBlob(hCur, pRecBuf, Index + 1, dbiREADWRITE));

       Check(DbiTruncateBlob(hCur, pRecBuf, Index + 1, 0));

       Check(DbiModifyRecord(hCur, pRecBuf, True));

       Check(DbiFreeBlob(hCur, pRecBuf, Index + 1));

    end;

  finally  // Close cloned cursor and free record buffer memory

    if (hCur <> nil) then

       Check(DbiCloseCursor(hCur));

     FreeMem(pRecBuf, BlobTbl.RecordSize);

  end;

end

else

  raise EDatabaseError.Create('Field: ' +

     BlobTbl.Fields[Index].FieldName + ', is not a blob type');

end;