- - * - WhiteUnicorn - * - -




* #WhiteUnicorn/ StartPage/ Documentation/DelphiFAQ >


2791:How to iterate through the fields of a table

KEYWORDS: Fields, Tables AREA: Database Programming

Getting a list of the fields in a table at run-time can be as simple as a
call to the GetFieldNames method of the TTable, TQuery, or TStoredProc
component. The DetFieldNames method returns a list of the fields that
comprise the structure of the data set in the form of a TStrings list,
which may be inserted into such visual components as a TListBox through
its Items property:

  ListBox1.Clear;
  Table1.GetFieldNames(ListBox1.Items);

Of course, the TStrings list returned by the GetFieldNames method need not
be used with a visual component. It could just as well serve as an array
of field names stored entirely in memory, that can be used as a list or
array.

But it is also possible to retrieve much more information about the
fields in a table than just the names. Other descriptive attributes incl-
ude field types and sizes. Retrieving tyhese values is slightly more
involved than the use of the GetFieldNames. Basically, this process
involves iterating through the FieldDefs property of the TTable, TQuery,
or TStoredProc component. The FieldDefs property is essentially an
array of records, one record for each field in the structure. Each field
record contains information about the field, including its name, type,
and size. It is a relatively straightforward process to iterate through
this array of field descriptions, extracting information about individual
fields.

There are a number of reasons why a program might need to query the
structure of a table used in the application. One reason is a prelude to
creating TField components at run-time that represent the fields in the
table. The information gleaned from the structure of the table form the
basis of the TField components to be created.

The example below demonstrates how to iterate through the fields available
in a TTable or TQuery. The example extracts information about the available
fields and displays the information in a TListBox, but the same methodology
can be used to provide information necessary for the dynamic building of
TField descendants. The example uses a TTable as the data set, but a TQuery
can be used in the same manner as both TTable and TQuery components incorp-
orate the Field-Defs property the same way.

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
  F: TFieldDef;
  D: String;
begin
  Table1.Active := True;
  ListBox1.Items.Clear;
  with Table1 do begin
    for i := 0 to FieldDefs.Count - 1 do begin
      F := FieldDefs.Items[i];
      case F.DataType of
        ftUnknown: D := 'Unknown';
        ftString: D := 'String';
        ftSmallint: D := 'SmallInt';
        ftInteger: D := 'Integer';
        ftWord: D := 'Word';
        ftBoolean: D := 'Boolean';
        ftFloat: D := 'Float';
        ftCurrency: D := 'Currency';
        ftBCD: D := 'BCD';
        ftDate: D := 'Date';
        ftTime: D := 'Time';
        ftDateTime: D := 'DateTime';
        ftBytes: D := 'Bytes';
        ftVarBytes: D := '';
        ftBlob: D := 'BLOB';
        ftMemo: D := 'Memo';
        ftGraphic: D := 'Graphic';
      else
        D := '';
      end;
      ListBox1.Items.Add(F.Name + ', ' + D);
    end;
  end;
  Table1.Active := False;
end;


        TI



* #WhiteUnicorn/ StartPage/ Documentation/DelphiFAQ >



- - * - Anastasija aka WhiteUnicorn - * - - LJLiveJournal
PFPhotoFile