* * *

Author Topic: TBufDataSet.Clear  (Read 614 times)

egsuh

  • New member
  • *
  • Posts: 14
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: 260
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, 256-SSD, Ubuntu 14.04-LTS, Unity Desktop, Lazarus 1.6.2, SQLite3 -- Retired: Programming for my own use for Ubuntu.

wp

  • Hero Member
  • *****
  • Posts: 3520
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;
Lazarus trunk / fpc 3.0.0 / Win32

Thaddy

  • Hero Member
  • *****
  • Posts: 3640
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 »
Why do the Danish always try to fuck up any programming language?

Thaddy

  • Hero Member
  • *****
  • Posts: 3640
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.
Why do the Danish always try to fuck up any programming language?

egsuh

  • New member
  • *
  • Posts: 14
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.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus