How to list the tables in a SQLite database file that was opened with ATTACH?

前端 未结 17 2237
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 11:33

What SQL can be used to list the tables, and the rows within those tables in an SQLite database file - once I have attached it with the

相关标签:
17条回答
  • 2020-11-22 12:03

    Use:

    import sqlite3
    
    TABLE_LIST_QUERY = "SELECT * FROM sqlite_master where type='table'"
    
    0 讨论(0)
  • 2020-11-22 12:04

    It appears you need to go through the sqlite_master table, like this:

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

    And then manually go through each table with a SELECT or similar to look at the rows.

    The .DUMP and .SCHEMA commands doesn't appear to see the database at all.

    0 讨论(0)
  • 2020-11-22 12:05

    There is a command available for this on the SQLite command line:

    .tables ?PATTERN?      List names of tables matching a LIKE pattern
    

    Which converts to the following SQL:

    SELECT name FROM sqlite_master
    WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%'
    UNION ALL
    SELECT name FROM sqlite_temp_master
    WHERE type IN ('table','view')
    ORDER BY 1
    
    0 讨论(0)
  • 2020-11-22 12:06

    To list the tables you can also do:

    SELECT name FROM sqlite_master
    WHERE type='table';
    
    0 讨论(0)
  • 2020-11-22 12:06

    The ".schema" commando will list available tables and their rows, by showing you the statement used to create said tables:

    sqlite> create table_a (id int, a int, b int);
    sqlite> .schema table_a
    CREATE TABLE table_a (id int, a int, b int);
    
    0 讨论(0)
  • 2020-11-22 12:07

    .da to see all databases - one called 'main'

    tables of this database can be seen by

    SELECT distinct tbl_name from sqlite_master order by 1;

    The attached databases need prefixes you chose with AS in the statement ATTACH e.g. aa (, bb, cc...) so:

    SELECT distinct tbl_name from aa.sqlite_master order by 1;

    Note that here you get the views as well. To exclude these add where type = 'table' before ' order'

    0 讨论(0)
提交回复
热议问题