Delphi: Reading number of columns + names from dataset?
dataset, delphi
Solution
Number of fields and their names could be acquired as follows:
procedure TForm1.Button1Click(Sender: TObject);
begin
with Query1 do
begin
ShowMessage(IntToStr(FieldCount));
ShowMessage(Fields[0].FieldName);
end;
end;
You can checkout `TFieldDef` for more detail info about the field.
`dataset.FieldDefs[0]` has properties like `DataType` and `Size`.
Problem
Since Embarcadero's NNTP server stopped responding since yesterday, I figured I could ask here: I work with a non-DB-aware grid, and I need to loop through a dataset to extract the number of columns, their name, the number of rows and the value of each fields in each row. I know to read the values for all the fields in each row, but I don't know how to extract column-related information. Does someone have some code handy? ``` 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; ``` Thank you.