Delphi: Reading number of columns + names from dataset?
Solution 1
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
.
Solution 2
If what you're looking for is a list of field names, try creating a TStringList and passing it to the TDataset.Fields.GetFieldNames procedure.
If you want more information about fields, the TFields object (ASQLite3Query1.Fields) has a default property and a Count property, so you can use it like an array, and an enumerator, both of which can be used to loop over each TField object and retrieve its metadata.
Gulbahar
Updated on July 12, 2020Comments
-
Gulbahar almost 4 years
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.