Delphi: Reading the number of columns + names from a dataset?

Since Embarcadero's NNTP server stopped responding from yesterday, I decided I could ask here: I am working on a network that does not support DB, and I need to scroll through the data set to extract the number of columns, their name, row number and values ​​of each field in each line.

I know how to read the values ​​for all the fields in each row, but I don't know how to retrieve the information associated with the column. Does anyone have a handy code?

procedure TForm1.FormCreate(Sender: TObject);
var
  index : Integer;
begin
  With ASQLite3DB1 do begin
      DefaultDir := ExtractFileDir(Application.ExeName);
      Database := 'test.sqlite';
      CharacterEncoding := 'STANDARD';
      Open;
  end;

  With ASQLite3Query1 do begin
    ASQLite3Query1.Connection := ASQLite3DB1;

    SQL.Text := 'CREATE TABLE IF NOT EXISTS mytable (id INTEGER PRIMARY KEY, label VARCHAR)';
    ExecSQL;

    SQL.Text := 'INSERT INTO mytable (label) VALUES ("dummy label")';
    ExecSQL;

    SQL.Text := 'SELECT id AS Identification, label AS Label FROM mytable';
    Open;

    //How to get column numbers + names to initialized grid object?
    for index := 0 to ASQLite3Query1. - 1 do begin

    end;

    for index := 0 to FieldCount - 1 do begin
      ShowMessage(Fields[index].AsString);
    end;
  end;
end;

Thanks.

+3
source share
2 answers

The number of fields and their names could be obtained as follows:

procedure TForm1.Button1Click(Sender: TObject);
begin
  with Query1 do
  begin
    ShowMessage(IntToStr(FieldCount));
    ShowMessage(Fields[0].FieldName);
  end;
end;

You can check TFieldDeffor more information about the field.

dataset.FieldDefs[0] , DataType Size.

+2

, TStringList TDataset.Fields.GetFieldNames.

, TFields (ASQLite3Query1.Fields) Count, , TField .

+1

Source: https://habr.com/ru/post/1712379/


All Articles