Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

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

What SQL can be used to list the tables, and the rows within those tables in an SQLite database file
I have attached it with the ATTACH command on the SQLite 3 command line tool?
by

2 Answers

kshitijrana14
The .tables, and .schema "helper" functions don't look into ATTACHed databases: they just query the SQLITE_MASTER table for the "main" database. Consequently, if you used

ATTACH some_file.db AS my_db;
then you need to do

SELECT name FROM my_db.sqlite_master WHERE type='table';
Note that temporary tables don't show up with .tables either: you have to list sqlite_temp_master for that:

SELECT name FROM sqlite_temp_master WHERE type='table';
Shahlar1vxp
The code for listing the tables in an SQLite database is as follows-
.tables ?PATTERN? List names of tables matching a LIKE pattern
Now, this code 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

Or you may also use this code-
SELECT name FROM sqlite_master
WHERE type='table';

Login / Signup to Answer the Question.