Recent

Author Topic: TBufDataSet.Clear  (Read 6943 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1297
TBufDataSet.Clear
« on: April 01, 2017, 06:11:45 pm »
The TBufDataSet.Clear seems the equivalent of EmptyDataSet or EmptyTable of Delphi, i.e. remove all records leaving the dataset structure. But this seems to remove field definitions as well. Or, it does nothing.

When I tested,

   BufDataSet1.Open;
   BufDataSet1.Clear;
   BufDataSet1.Open;

There are no change in data. The DBGrid is the same.

But if I do:

   BufDataSet1.Close;
   BufDataSet1.Clear;
   BufDataSet1.Open;

Then the last command raise an error. No dataset exist or something.

Can anyone explain why to me?

HatForCat

  • Sr. Member
  • ****
  • Posts: 293
Re: TBufDataSet.Clear
« Reply #1 on: April 01, 2017, 06:35:44 pm »
Can't help with the explanation but I have written a generic procedure that I use to Empty a table.

I too come from Win/Delphi!! :)

Code: Pascal  [Select][+][-]
  1. procedure TableEmpty(const aQry : TSQLQuery; const aTrans : TSQLTransaction);
  2. begin
  3.   if aQry.RecordCount > 0 then
  4.   begin
  5.     aQry.SQL.Clear;
  6.     aQry.SQL.Text:='DELETE FROM '+aQry.Name;
  7.     aQry.ExecSQL;
  8.   end;
  9.   aQry.ApplyUpdates;
  10.   aTrans.Commit;
  11. end;
  12.  
Acer-i5, 2.6GHz, 6GB, 500GB-SSD, Mint-19.3, Cinnamon Desktop, Lazarus 2.0.6, SQLite3

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: TBufDataSet.Clear
« Reply #2 on: April 01, 2017, 06:41:29 pm »
This is what BufDataset.Clear does:
Code: Pascal  [Select][+][-]
  1. procedure TCustomBufDataset.Clear;
  2. begin
  3.   Close;
  4.   FieldDefs.Clear;
  5.   Fields.Clear;
  6. end;

I don't think that HatForCat's code works for TBufDataset because it does not understand SQL. Instead iterate through all records and delete them (untested...):
Code: Pascal  [Select][+][-]
  1. procedure ClearAllRecords(ADataset: TDataset);
  2. begin
  3.   ADataset.DisableControls;
  4.   try
  5.     ADataset.First;
  6.     while not ADataset.EoF do
  7.       ADataset.Delete;
  8.   finally
  9.     ADataset.EnableControls;
  10.   end;
  11. end;

Thaddy

  • Hero Member
  • *****
  • Posts: 14382
  • Sensorship about opinions does not belong here.
Re: TBufDataSet.Clear
« Reply #3 on: April 01, 2017, 06:49:23 pm »
Calling clear on a TBufDataset deletes its content from memory and resets *everything*.
You probably forgot to save to file or save (commit) to an underlying dataset.
The reason is that the Field Defs are cleared too.
The fastest way  I know in your case is to copy the fielddefs to a temp TFielddefs, call clear and re-assign the fielddefs.
Something like:
Code: Pascal  [Select][+][-]
  1.   // your code first
  2.    OldFieldDefs := TFielddefs.Create(nil);
  3.    OldFieldDefs.Assign(BufDb.FieldDefs);
  4.    BufDb.Clear;
  5.    BufDb.Close;
  6.    BufDb.FieldDefs.Assign(OldFieldDefs);
  7.    BufDb.CreateDataset;
  8.    BufDb.Open;
  9.    OldFieldDefs.Free;
« Last Edit: April 01, 2017, 08:12:20 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Thaddy

  • Hero Member
  • *****
  • Posts: 14382
  • Sensorship about opinions does not belong here.
Re: TBufDataSet.Clear
« Reply #4 on: April 01, 2017, 10:18:59 pm »
I did some tests. wp's code is faster than my code, so use that.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

egsuh

  • Hero Member
  • *****
  • Posts: 1297
Re: TBufDataSet.Clear
« Reply #5 on: April 04, 2017, 11:23:08 pm »
Thank all of you for your comments. I may use wp's method. Just interested as  TBufDataSet provides ClearFields method as well.

Anspach

  • Newbie
  • Posts: 1
Re: TBufDataSet.Clear
« Reply #6 on: July 17, 2017, 08:05:01 am »
Thank all of you for your comments. I may use wp's method. Just interested as  TBufDataSet provides ClearFields method as well.

Thanks for the code wp, worked great!
« Last Edit: October 20, 2021, 06:21:41 pm by Anspach »

 

TinyPortal © 2005-2018