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 ATTACH command on the sqlite3 command line tool?
19 Answers
There are a few steps to see the tables in an SQLite database:
List the tables in your database:
.tablesList how the table looks:
.schema tablenamePrint the entire table:
SELECT * FROM tablename;List all of the available SQLite prompt commands:
.help
7 Comments
.table and .tables are both allowed. For that matter, .ta would work as well, since sqlite3 will accept any command that is unambiguous. The name of the command according to the help is indeed ".tables" (if anyone is still paying attention)..tables won't display tables if one opened database(s) through ATTACH '<path>' AS <name>;but lasse's answer will do. since the OP mentioned ATTACHing i believe he was right in not accepting this answer. edit: just noticed that anthony and others below also pointed this out.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';
6 Comments
"SELECT name FROM sqlite_master WHERE type='table'" works for meCREATE TEMPORARY TABLE SQL commands. Their contents are dropped when the current database connection is closed, and they are never saved to a database file.ATTACH "some_file.db" AS my_db; It worked!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.
3 Comments
sqlite_master table from column type where row is table right? I'd suggest you simply add that in your answer to make everyone better understand the structure of a database and its master tableTo show all tables, use
SELECT name FROM sqlite_master WHERE type = "table"
To show all rows, I guess you can iterate through all tables and just do a SELECT * on each one. But maybe a DUMP is what you're after?
1 Comment
Use .help to check for available commands.
.table
This command would show all tables under your current database.
2 Comments
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
2 Comments
.tables ?PATTERN? List names of tables matching a LIKE pattern", has been lifted (wholesale copied) from the official documentation/help output without attribution..tables "%"; command does the trick, and shows all tables, and so does .table.To list the tables you can also do:
SELECT name FROM sqlite_master
WHERE type='table';
2 Comments
cur.execute(""" SELECT name FROM sqlite_master WHERE type='table';""") or no? That's not working for me, but I'm not sure where this code is supposed to be run.Try PRAGMA table_info(table-name);
http://www.sqlite.org/pragma.html#schema
1 Comment
According to the documentation, the equivalent of MySQL's SHOW TABLES; is:
The ".tables" command is similar to setting list mode then executing the following query:
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;
However, if you are checking if a single table exists (or to get its details), see LuizGeron's answer.
Comments
As of the latest versions of SQLite 3 you can issue:
.fullschema
to see all of your create statements.
2 Comments
Use .da to see all databases - one is 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'
Comments
The easiest way to do this is to open the database directly and use the .dump command, rather than attaching it after invoking the SQLite 3 shell tool.
So instead of in the SQLite 3 shell tool,
ATTACH database.sqlite as "attached"
from your OS command line, open the database directly:
sqlite3 database.sqlite
And in the shell tool:
.dump
Comments
I think it may be useful to refer to the official reference of SQLite under this heading:
You can manipulate your database using the commands described in there. Besides, if you are using Windows and do not know where the command shell is, that is on the SQLite site's download page.
After downloading it, click the sqlite3.exe file to initialize the SQLite command shell. When it is initialized, by default this SQLite session is using an in-memory database, not a file on disk, and so all changes will be lost when the session exits. To use a persistent disk file as the database, enter the ".open ex1.db" command immediately after the terminal window starts up.
The example above causes the database file named "ex1.db" to be opened and used, and created if it does not previously exist. You might want to use a full pathname to ensure that the file is in the directory that you think it is in. Use forward slashes as the directory separator character. In other words use "c:/work/ex1.db", not "c:\work\ex1.db".
To see all tables in the database you have previously chosen, type the command .tables as it is said in the above link.
If you work in Windows, I think it might be useful to move this sqlite.exe file to the same folder with the other Python files. In this way, the Python file writes to and the SQLite shell reads from .db files are in the same path.
Comments
To get a list of tables in a SQLite database, you can use a simple SQL query. In SQLite, there's a table called sqlite_master that stores metadata about the database schema, including the table names. You can query this table to retrieve the names of all tables in the database.
Let's assume, for example, we have two tables, table1 and table2 in the SQLite database.
Here's the SQL query to fetch the list of tables in the SQLite database:
SELECT name FROM sqlite_master WHERE type='table';
Output:
[('table1',), ('sqlite_sequence',), ('table2',)]
Filter the results to only include entries of type 'table' while excluding internal SQLite tables (those starting with sqlite_).
SELECT name FROM sqlite_master
WHERE type='table' AND name NOT LIKE 'sqlite_%';
Output:
[('table1',), ('table2',)]
Comments
Since the questioner did not provide a minimal reproducible example, I'll include a possible one in the following steps:
attach secondary in-memory database as
auxcreate a table
numbersin the attached database (withschema-nameset toaux)inspect metadata of
numbersinauxvia PRAGMA table_xinfoATTACH DATABASE 'file::memory:' AS aux; CREATE TABLE aux.numbers (v INT, name TEXT); PRAGMA aux.table_xinfo(numbers);Like all pragmas,
pragma table_xinfocan also be used in form of a pragma function. We select its results frompragma_table_xinfo. Please note that the table name argument must be a string literal here.SELECT * FROM aux.pragma_table_xinfo('numbers');
Here is the output of both query variants (jazzed up by .mode box):
┌─────┬──────┬──────┬─────────┬────────────┬────┬────────┐
│ cid │ name │ type │ notnull │ dflt_value │ pk │ hidden │
├─────┼──────┼──────┼─────────┼────────────┼────┼────────┤
│ 0 │ v │ INT │ 0 │ │ 0 │ 0 │
│ 1 │ name │ TEXT │ 0 │ │ 0 │ 0 │
└─────┴──────┴──────┴─────────┴────────────┴────┴────────┘
You may be interested in the fact that you can get meta information about the pragma “tables” themselves, try pragma table_xinfo(pragma_xinfo); if you're curious.
.tablesfor tables and.schema ?TABLE?for the schema of the specific table..table 'bank_%'or.table '%_empl'also valid syntax for quering prefixes/suffixes!.tablesand.schemadeliver SQL (including original formatting and comments) that is often not as useful for further processing. To get the exact metadata, use pragmas or pragma functions, my answer demonstrates the use of both.