Recent

Author Topic: Should TCSVDataset.Clear empty the CSV-file?  (Read 1370 times)

Vodnik

  • Full Member
  • ***
  • Posts: 210
Should TCSVDataset.Clear empty the CSV-file?
« on: February 22, 2023, 09:45:27 pm »
Hello!
Attached is example published by wp in https://forum.lazarus.freepascal.org/index.php/topic,60714.msg455146.html#msg455146
When CSVDataset1.Clear is called by "Clear all records" button, records are deleted.
I expected that if application is closed at this step, empty CSV file should be saved.
But this is not the case. If then call CSVDataset1.Open, original CSV file will be loaded.
Is this intended behaviour?
What is the way to really delete all data from TCSVDataset?
(By the way, deleting single record doesn't work in this example, too)
Lazarus 2.2.4 for Windows

paweld

  • Hero Member
  • *****
  • Posts: 1003
Re: Should TCSVDataset.Clear empty the CSV-file?
« Reply #1 on: February 23, 2023, 11:10:05 am »
CSVDataset1.Clear - clears only the definition of CSVDataset1 fields.
If you want to delete all records then you need to perform something like this: 
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. var
  3.   i: Integer;
  4. begin
  5.   CSVDataset1.DisableControls;
  6.   CSVDataset1.First;
  7.   for i := CSVDataset1.RecNo to CSVDataset1.RecordCount do
  8.     CSVDataset1.Delete;
  9.   CSVDataset1.EnableControls;
  10.   CSVDataset1.SaveToCSVFile('exported.csv');
  11. end;
  12.  
« Last Edit: February 23, 2023, 07:18:15 pm by paweld »
Best regards / Pozdrawiam
paweld

Vodnik

  • Full Member
  • ***
  • Posts: 210
Re: Should TCSVDataset.Clear empty the CSV-file?
« Reply #2 on: February 23, 2023, 03:42:09 pm »
Thank you for an example, paweld.
It does not work with TCSVDataset.
"Applying updates is not supported by this TDataset descendent"
After removing ApplyUpdates:
"CSVDataset1: Must apply updates before refreshing data"
After removing Refresh:
Compiles successfully, application runs, data disappear from CSVDataset, but not from the file.
The same when using DBNavigator: records can be added, but not removed.
And Clear is defined as:
Code: Pascal  [Select][+][-]
  1. procedure TCustomBufDataset.Clear;
  2. begin
  3.   Close;
  4.   FieldDefs.Clear;
  5.   Fields.Clear;
  6. end;
  7.  
So I expect it should clear the fields, too.



paweld

  • Hero Member
  • *****
  • Posts: 1003
Re: Should TCSVDataset.Clear empty the CSV-file?
« Reply #3 on: February 23, 2023, 07:19:05 pm »
Sorry, I pasted the wrong code. already corrected the previous post.
Best regards / Pozdrawiam
paweld

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: Should TCSVDataset.Clear empty the CSV-file?
« Reply #4 on: February 23, 2023, 07:42:45 pm »
And Clear is defined as:
Code: Pascal  [Select][+][-]
  1. procedure TCustomBufDataset.Clear;
  2. begin
  3.   Close;
  4.   FieldDefs.Clear;
  5.   Fields.Clear;
  6. end;
  7.  
So I expect it should clear the fields, too.
It does. But the dataset is closed in the first line where it writes the records to file. Afterwards the table is emptied, but this cannot be written to the file because the dataset is closed now.

I think this method is not intended for removing the records because it clears also the field defs, i.e. it empties the entire table.

I think the main problem here is that it is not possible to delete records in the CSVDataset. Something is missing for this purpose. Maybe the PacketWriter is incomplete; the packet reader/writer classes for the CSV format are the main differences to the parent TBufdataset class. Please file a bug report.

Vodnik

  • Full Member
  • ***
  • Posts: 210
Re: Should TCSVDataset.Clear empty the CSV-file? [BUG REPORTED]
« Reply #5 on: February 26, 2023, 02:51:41 pm »
wp, thank You for processing my bug report!

paweld, your code works, but delete operation causes memory leak. Seems there may be more problems in CSVDataset realization.

I'm stuck with my project on this problem.
I've to look for more reliable in-memory dataset, capable to load/store data from/to CSV-file.

egsuh

  • Hero Member
  • *****
  • Posts: 1296
Re: Should TCSVDataset.Clear empty the CSV-file?
« Reply #6 on: February 26, 2023, 04:12:22 pm »
We need a method EmptyDataset which is supported by TClientDataset of Delphi.

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Should TCSVDataset.Clear empty the CSV-file?
« Reply #7 on: February 26, 2023, 07:24:02 pm »
We need a method EmptyDataset which is supported by TClientDataset of Delphi.

Nice naming again from the Greek.
Sounds more like a method returning a Boolean.

Bart

 

TinyPortal © 2005-2018