Inspect tables in an attached SQLite database
An attached SQLite database has its own schema name and catalog. Query archive.sqlite_schema to inspect the database attached as archive. An unqualified catalog query does not mean “search every attached database”.
Listing table definitions and reading table rows are also different operations. The catalog tells you which objects exist. Once you select an object, a normal schema-qualified SELECT reads its contents.
Create a disposable two-database fixture
Start a fresh in-memory connection, for example with sqlite3 :memory:. Run the following statements in that same connection. Both the main database and the attached database are in memory.
CREATE TABLE main.Catalog (Id INTEGER PRIMARY KEY, Label TEXT);
INSERT INTO main.Catalog (Id, Label) VALUES (1, 'main item');
ATTACH DATABASE ':memory:' AS archive;
CREATE TABLE archive.Catalog (Id INTEGER PRIMARY KEY, Label TEXT);
CREATE TABLE archive.Events (Id INTEGER PRIMARY KEY, Kind TEXT);
INSERT INTO archive.Catalog (Id, Label) VALUES (20, 'archived item');
PRAGMA database_list;
The database list includes main and archive. Their filename fields are empty for these in-memory databases. Do not rely on a particular numeric sequence value for archive, because other attached or temporary schemas can affect the inventory.
For a database already attached by your application, skip fixture creation and use its actual schema alias. The alias is a connection-local name, not necessarily its filename. An attachment created on one pooled connection is not automatically present on another.
List tables in the correct catalog
SELECT name
FROM archive.sqlite_schema
WHERE type = 'table'
AND name NOT GLOB 'sqlite_*'
ORDER BY name;
The result is Catalog followed by Events. Filtering by type excludes views, indexes and triggers. The GLOB pattern excludes reserved internal names beginning with the literal prefix sqlite_. Unlike a LIKE underscore, that underscore is not a single-character wildcard in this pattern.
Older examples often use sqlite_master. SQLite documents it as an alternative name for the same schema table. Prefer the descriptive sqlite_schema name in new material, while recognizing the older spelling when maintaining existing queries.
To inspect definitions, select name, sql from the same catalog. Treat this as inspection data. Do not update the schema table to rename or repair objects, and do not blindly execute DDL obtained from an unfamiliar database.
Read the selected table explicitly
SELECT Id, Label FROM main.Catalog ORDER BY Id;
SELECT Id, Label FROM archive.Catalog ORDER BY Id;
PRAGMA archive.table_info('Catalog');
The first query returns id 1 and main item. The second returns id 20 and archived item. Explicit qualification avoids ambiguity when databases contain tables with identical names. table_info describes the ordinary columns rather than returning the data.
Use table_xinfo when generated or hidden columns matter. On SQLite 3.37 or later, PRAGMA archive.table_list; provides additional object information and distinguishes ordinary, virtual and shadow tables. A simple catalog query can include virtual-table backing objects, so choose the inventory method that matches what you intend to display.
Keep client commands and SQL separate
Commands such as .tables and .schema belong to the SQLite command-line shell. They are useful interactively but cannot be sent as SQL through a driver. Catalog queries and supported PRAGMAs work through the SQL interface.
Bind runtime values where a statement accepts values. Schema and table identifiers require deliberate selection and quoting rather than value parameters. When building an inspection tool, choose identifiers from the discovered catalog and escape embedded identifier quotes correctly.
Finish this fixture after all statements and result readers have completed.
DETACH DATABASE archive;
Detaching removes the alias from this connection. It does not delete a database file. In this particular fixture the attached in-memory database disappears, while closing the remaining connection discards main as well.
Source and further reading
This article addresses the problem raised by izb in “How can I list the tables in a SQLite database file that was opened with ATTACH?” on Stack Overflow, licensed under CC BY-SA 4.0 . The question is the topic source. The explanations and examples here are newly authored from the database documentation, rather than reproduced from a community answer.
Author: Herman Schoenfeld