Should TCSVDataset.Clear empty the CSV-file? lazarus / 16-Databases ---------------------------------------------------------------------- Vodnik · 2023-02-22 20:45 Hello! Attached is example published by wp in https://forum.lazarus.freepasca l.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 · 2023-02-23 10:10 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button2Click(Sender: TObject);var  i: Integer;begin  CSVDataset1.DisableControls;  CSVDataset1.First;  for i := CSVDataset1.RecNo to CSVDataset1.RecordCount do    CSVDataset1.Delete;  CSVDataset1.EnableControls;  CSVDataset1.SaveToCSVFile('exported.csv');end; ---------------------------------------------------------------------- Vodnik · 2023-02-23 14:42 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TCustomBufDataset.Clear;begin  Close;  FieldDefs.Clear;  Fields.Clear;end; So I expect it should clear the fields, too. ---------------------------------------------------------------------- paweld · 2023-02-23 18:19 Sorry, I pasted the wrong code. already corrected the previous post. ---------------------------------------------------------------------- wp · 2023-02-23 18:42 Quote from: Vodnik on February 23, 2023, 03:42:09 pmAnd Clear is defined as: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TCustomBufDataset.Clear;begin  Close;  FieldDefs.Clear;  Fields.Clear;end; 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 · 2023-02-26 13:51 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 · 2023-02-26 15:12 We need a method EmptyDataset which is supported by TClientDataset of Delphi. ---------------------------------------------------------------------- Bart · 2023-02-26 18:24 Quote from: egsuh on February 26, 2023, 04:12:22 pmWe 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