Page 1 of 1

Tables, Views

Posted: Fri 22 Jun 2012 12:16
by braune
Hello,

how can i get only tables and views for a database.

Code: Select all

  List := TSringList.Create;
  try
    Conn.Server := 'Server';
    Conn.Port := 3306;
    Conn.Username := 'root';
    Conn.Password := '****';
    Conn.Database := 'DBName';
    Conn.Open;
    Conn.GetTableNames(List);
    // In the List views are available.
    // How can i get views ?? Conn.GetViewNames not available
  finally
    List.Free;
  end;
Thanks for your help.

Re: Tables, Views

Posted: Fri 22 Jun 2012 14:52
by AlexP
hello,

Using the GetTableNames method you cannot separate views from tables, for this you can use the TMyMetaData component in the following

Code: Select all

var
  MyMetaData: TMyMetaData;
  List: TStringList;
begin
  MyMetaData := TMyMetaData.Create(nil);
  try
    MyMetaData.Connection := MyConnection1;
    MyMetaData.MetaDataKind := 'Tables';
    MyMetaData.Restrictions.Values['TABLE_TYPE'] := 'VIEW';

    List := TStringList.Create;
    try
      MyMetaData.Open;
      while not MyMetaData.eof do
      begin
        List.Add(MyMetaData.FieldByName('TABLE_NAME').AsString);
        MyMetaData.next;
      end;
    finally
      List.Free;
    end;
  finally
    MyMetaData.Free;
  end;
where

Code: Select all

MyMetaData.Restrictions.Values['TABLE_TYPE'] := 'VIEW'; <- only views are selected
MyMetaData.Restrictions.Values['TABLE_TYPE'] := 'BASE TABLE'; <- only tables are selected

Re: Tables, Views

Posted: Sat 23 Jun 2012 09:52
by braune
Hello,

easy, ... thanks for your help

Re: Tables, Views

Posted: Sat 23 Jun 2012 09:57
by AlexP
hello,

If you have any other questions, feel free to contact us.