How to use FireDAC TFDConnection to iterate over tables, fields, and field definitions

I am teaching myself to use SQLite and FireDAC together in Delphi. I don’t know much about the latest incarnations of databases and tools, so by writing a very simple application to display a single table from an SQLite file, I decided that I would put together a simple “frame” of the viewer that will help me learn and maybe give me ( ultimately) a debugging tool to put into my application for engineering use.

So, I used a simple TTreeView and want to populate it with a hierarchy of “databases” (directories?), “Tables”, “field names” and “field types”. Until now, it has been remarkably easy to list directories, tables, and fields (using TFDConnection.Getxxxxx), but I can't figure out how to go deeper into defining fields. Can this be done from TFDConnection? Or do I need to open a temporary request?

My existing code is shown below, and my "field types" will be an extra nested loop when it displays as "// xxxxxxxxxxxxxxxxxxx"

procedure TForm1.Button1Click(Sender: TObject);

  procedure DatabaseToTreeView( AConnection : TFDConnection; ATreeView : TTreeView );

    procedure ProcessConnection;

      procedure ProcessCatalogueName( const ACatalogueName : string; ARoot : TTreeNode );

        procedure ProcessTableName( const ATableName : string; ARoot : TTreeNode );
        var
          List : TStrings;
          {Node : TTreeNode;}
          I    : integer;
        begin
          List := TStringList.Create;
          try
            AConnection.GetFieldNames( ACatalogueName, '', ATableName, '', List );
            for I := 0 to List.Count-1 do
              begin
              {Node := }ATreeView.Items.AddChild( ARoot, List[I] );
              // xxxxxxxxxxxxxxxxxxx
              end;
          finally
            List.Free;
          end;
        end;

      var
        List : TStrings;
        Node : TTreeNode;
        I    : integer;
      begin
        List := TStringList.Create;
        try
          AConnection.GetTableNames( ACatalogueName, '', '', List );
          for I := 0 to List.Count-1 do
            begin
            Node := ATreeView.Items.AddChild( ARoot, List[I] );
            ProcessTableName( List[I], Node );
            end;
        finally
          List.Free;
        end;
      end;


    var
      List : TStrings;
      Node : TTreeNode;
      I    : integer;
    begin
        List := TStringList.Create;
        try
          AConnection.GetCatalogNames( '', List );

          if List.Count = 0 then
            ProcessCatalogueName( '', nil )
           else
            for I := 0 to List.Count-1 do
              begin
              Node := ATreeView.Items.AddChild( nil, List[I] );
              ProcessCatalogueName( List[I], Node );
              end;
        finally
          List.Free;
        end;
    end;


  begin
    ATreeView.Items.Clear;
    ATreeView.Items.BeginUpdate;
    try
      ProcessConnection;
    finally
      ATreeView.Items.EndUpdate;
    end;
  end;

begin
  FDConnection1.Open;
  FDQuery1.Active := true;

  DatabaseToTreeView( FDConnection1, TreeView1 );

end;

Thanks a lot, Brian.

+4
source share
3 answers

- TFDTable, AConnection FieldDefs.Update. .

+1
+1

- (, "select * from tablename, 1 = 0" - , , ), , . ;

MySQL:

show columns from  tablename;

SQLite:

PRAGMA table_info(tablename)

MS SQL Server:

select column_name, data_type, character_maximum_length from INFORMATION_SCHEMA.COLUMNS where table_name = 'tablename';

, PostgreSQL ​​, pgsql.

0
source

All Articles