Search code examples
sqlitedelphifiremonkeyfiredac

Delphi FDQuery selecting rows without the column name


I have some code on a form which creates a bunch of labels from the column names in the database

I am trying to write another piece of code that uses a FDQuery to select all the records and then create labels based on the value in each row

Currently i have the following.

  while not FDQuery1.Eof do
  begin
    while recordCount < colTotal do
    begin
      newLabel := TLabel.Create(Self);
      newLabel.Parent := panel3;
      newLabel.Align := TAlignLayout.Top;
      newLabel.Text := FDQuery1.FieldByName('Torque2').AsString;
      newLabel.Margins.Top := 10;
      newLabel.Margins.Left := 10;
      newLabel.Margins.Right := 10;
      inc(recordCount);
      FDQuery1.Next;
    end;
  end;

How can i make this create a label with the result of each row dynamically without me needing to actually put the column name like "torque2" as i have here.

So for example on my form the labels will create as follows

row1 
row2 
row3
row4
row5 

because right now this code just simply loops one row value

Thanks


Solution

  • Instead of name based FieldByName method you can access tuple values by the Fields property which is a 0 based indexed collection property.

    FireDAC offers more efficient way for accessing data though. Once you have all tuples fetched on the client, you can iterate through the internal data storage this way:

    var
      S: string;
      Row, Col: Integer;
    begin
      for Row := 0 to FDQuery1.Table.Rows.Count - 1 do
        for Col := 0 to FDQuery1.Table.Columns.Count - 1 do
          S := FDQuery1.Table.Rows[Row].GetData(Col);
    end;
    

    That's IMHO easier to read and also saves quite a lot of time because it doesn't move the dataset cursor. Disadvantage might be that you need to have tuples fetched on the client side.