Forum > Databases

Should TCSVDataset.Clear empty the CSV-file?

(1/2) > >>

Vodnik:
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:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TCustomBufDataset.Clear;begin  Close;  FieldDefs.Clear;  Fields.Clear;end; So I expect it should clear the fields, too.


paweld:
Sorry, I pasted the wrong code. already corrected the previous post.

wp:

--- Quote from: Vodnik on February 23, 2023, 03:42:09 pm ---And Clear is defined as:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TCustomBufDataset.Clear;begin  Close;  FieldDefs.Clear;  Fields.Clear;end; So I expect it should clear the fields, too.

--- End quote ---
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.

Navigation

[0] Message Index

[#] Next page

Go to full version