How to list all tables in a SQLite database?

4

In MySQL, when I run the command SHOW TABLES I can see all the tables present in a particular database.

What about SQLite? How can I do this?

    
asked by anonymous 15.12.2016 / 15:52

4 answers

5

If you are using the "interpreter" it can use the .tables or schema commands to list the tables or their structures.

If you want to do this in code and want a result that can be used for something in your application, refer to the database data dictionary where you have all tables:

SELECT * FROM sqlite_master WHERE type='table';

WHERE is required because there you also have indexes. If you want only some data, you can filter, choose the columns, whatever you want, you already know how to use SQL. Just do not try to modify this and other control tables because there are no triggers that process everything that is needed.

If you test that the structure is written as text, it puts the command that was used to create the table. It is not very practical to consult, but the information is there.

If you do a ATTACH you need to tell what database you are referring to. Example:

ATTACH novoBanco.db AS novo;

SELECT * FROM novo.sqlite_master WHERE type='table';

If you need temporary tables, you need to look at another location: sqlite_temp_master .

This is in the official FAQ .

One thing that few know is that all internal database control is usually in regular tables with special hardcoded access.

    
15.12.2016 / 16:09
2

Let's say your db is called MEUDB.

You can run the following command:

SELECT name FROM meudb.sqlite_master WHERE type='table';
    
15.12.2016 / 16:09
2
  • List the database tables:

    .tables

  • Show table details:

    .schema tablename

  • Show all records in the table:

    SELECT * FROM tablename;

  • List of all SQLite commands (terminal / prompt):

    .help

  • Source

        
    15.12.2016 / 16:22
    1

    sqlite3 fffffff.sqlite .tables

        
    15.12.2016 / 16:55