Recent

Author Topic: TCSVDataset FieldDefs defaults to ftString, 255 when reading from CSV-file  (Read 484 times)

Vodnik

  • Full Member
  • ***
  • Posts: 210
I can't succeed defining  CSVDataset1.FieldDefs in case when dataset is linked to existing CSV-file.
When dataset is opened, FieldDefs are overwritten with DataType=ftString and Size=255.
Is it a bug, an idea or I'm doing something wrong?
Test project  attached.
First run - no CSV file - FieldDefs are OK - CSV file is created accordingly.
Second run - CSV file exist - FieldDefs are default - record appended to CSV in wrong format.

Code: Pascal  [Select][+][-]
  1. program TestCSVDS;
  2.  
  3. uses csvdataset, sysutils, db;
  4.  
  5. var  CSVDataSet1: TCSVDataSet;
  6.      CSVFileName: String = 'data.csv';
  7.      I: Integer;
  8.  
  9. begin
  10.   CSVDataset1:=TCSVDataset.Create(nil);
  11.   try
  12.     CSVFileName:=ExtractFilePath(Paramstr(0)) + CSVFileName;
  13.     CSVDataset1.FileName:=CSVFileName;
  14.     CSVDataset1.CSVOptions.FirstLineAsFieldNames:=False;
  15.     CSVDataset1.FieldDefs.Add('Date and time', ftDateTime);
  16.     CSVDataset1.FieldDefs.Add('Duration', ftTime);
  17.     CSVDataset1.FieldDefs.Add('Event', ftString,40);
  18.      if FileExists(CSVFileName) then begin
  19.        Writeln('File '+CSVFileName+' exist');
  20.      end else begin
  21.        CSVDataset1.CreateDataset;
  22.        Writeln('File '+CSVFileName+' not exist');
  23.      end;
  24.     CSVDataset1.Open;
  25.     CSVDataset1.AppendRecord([EncodeDate(2023,03,03),EncodeTime(13,08,01,000),'Oleg']);
  26.     for I:= 0 to csvdataset1.FieldDefs.Count-1 do begin
  27.       Writeln('Record: ',I,', data type: ',csvdataset1.FieldDefs.Items[I].DataType,', data size: ', csvdataset1.FieldDefs.Items[I].Size);
  28.     end;
  29.     CSVDataset1.Close;
  30.     CSVDataset1.Free;
  31.     Readln;
  32.   except
  33.  
  34.   end;
  35. end.
  36.  

File data.csv after 2 runs:
Code: Pascal  [Select][+][-]
  1. 03.03.2023,13:08:01,Oleg
  2. 44988,"0,547233796296296",Oleg
« Last Edit: March 02, 2023, 10:21:56 am by Vodnik »

tatamata

  • Hero Member
  • *****
  • Posts: 787
    • ZMSQL - SQL enhanced in-memory database
Thank you for asking this question, I have noticed and wondering about the same thing while working on new ZMSQL version. Curious to see answers.
« Last Edit: March 02, 2023, 04:24:26 pm by tatamata »

 

TinyPortal © 2005-2018