Recent

Author Topic: What am I doing wrong with TSdfDataset ?  (Read 3540 times)

Tommi

  • Full Member
  • ***
  • Posts: 198
What am I doing wrong with TSdfDataset ?
« on: April 27, 2014, 08:10:39 pm »
Hi, I don't understand why this code crashes:
Code: [Select]
  FDataset := TSdfDataset.Create(nil);
  s:='Date;Time;A;B;C;D;E'+#13+'1923,06,21;07:00;1111;2222;3333;4444;5555';
  t:=TStringStream.Create(s);
  FDataset.LoadFromStream(t);
  FDataset.FieldDefs.Add('Date', ftString);
  //schema addition is needed so that sdfdataset writes out the csv header correctly:
  FDataset.Schema.Add('Date');
  FDataset.FieldDefs.Add('Time', ftString);
  FDataset.Schema.Add('Time');
  FDataset.FieldDefs.Add('A', ftString);
  FDataset.Schema.Add('A');
  FDataset.FieldDefs.Add('B', ftString);
  FDataset.Schema.Add('B');
  FDataset.FieldDefs.Add('C', ftString);
  FDataset.Schema.Add('C');
  FDataset.FieldDefs.Add('D', ftString);
  FDataset.Schema.Add('D');
  FDataset.FieldDefs.Add('E', ftString);
  FDataset.Schema.Add('E');
  FDataset.Delimiter := ';';     <<----------- Crashes here
  FDataset.FirstLineAsSchema := True;

If instead of using
Code: [Select]
   FDataset.LoadFromStream(t);
I use:
Code: [Select]
   FDataset.FileName:='....';

everything works perfectly.

howardpc

  • Hero Member
  • *****
  • Posts: 3977
Re: What am I doing wrong with TSdfDataset ?
« Reply #1 on: April 27, 2014, 09:06:10 pm »
Providing a schema is an alternative to adding fielddefs in code. Mixing the two is misguided. Probably what you are after is something like this
Code: [Select]
begin
  FDataset := TSdfDataset.Create(nil);
  try
    FDataset.Delimiter := ';';
    FDataset.FieldDefs.Add('Date', ftString);
    FDataset.FieldDefs.Add('Time', ftString);
    FDataset.FieldDefs.Add('A', ftString);
    FDataset.FieldDefs.Add('B', ftString);
    FDataset.FieldDefs.Add('C', ftString);
    FDataset.FieldDefs.Add('D', ftString);
    FDataset.FieldDefs.Add('E', ftString);
    s:= '1923,06,21;07:00;1111;2222;3333;4444;5555';
    t:=TStringStream.Create(s);
    try
      FDataSet.LoadFromStream(t);
    finally
      t.Free;
    end;
  finally
    // do something with FDataSet...
    FDataSet.Free;
  end;
end;       

Tommi

  • Full Member
  • ***
  • Posts: 198
Re: What am I doing wrong with TSdfDataset ?
« Reply #2 on: April 27, 2014, 09:32:03 pm »
I mixed them because I found it in this wiki:  http://wiki.freepascal.org/CSV

In any case as you suggest in your example the problem was that FDataset.LoadFromStream(t); has to be after and not before
Code: [Select]
FDataset.Delimiter := ';';
 FDataset.FieldDefs.Add('Date', ftString);
  .......

Now it works, thank you :)

howardpc

  • Hero Member
  • *****
  • Posts: 3977
Re: What am I doing wrong with TSdfDataset ?
« Reply #3 on: April 27, 2014, 10:39:10 pm »
IMHO the wiki article you looked at is itself confused about schemas.
I'll leave it to BigChimp to alter the wiki if he thinks it's needed. However in my view an example showing how to use manually coded fielddefs, and an example showing the use of a schema as an alternative approach would be clearer and more instructive. Perhaps you could offer such yourself?

Tommi

  • Full Member
  • ***
  • Posts: 198
Re: What am I doing wrong with TSdfDataset ?
« Reply #4 on: April 28, 2014, 03:03:49 pm »
Yes, at the moment I haven't the code with me but tomorrow I'll post it.

 

TinyPortal © 2005-2018