1264

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 SQLite 3 command line tool?

Arsen Khachaturyan
  • 6,472
  • 4
  • 32
  • 36
izb
  • 45,586
  • 39
  • 110
  • 165
  • 1
    try this one you got full info of tables [http://www.sqlite.org/pragma.html#schema](http://www.sqlite.org/pragma.html#schema) – Piyush Sep 09 '11 at 07:39
  • 3
    The following is a useful GUI for sqlite if you are interested: http://sqlitestudio.pl/ Gives you access to view the details of the databases, tables, very quickly and has a nice query editor too... – James Oravec Apr 17 '13 at 15:40
  • 19
    `.tables` for tables and `.schema ?TABLE?` for the schema of the specific table. – H6. Jun 16 '13 at 15:52
  • `.table 'bank_%'` or `.table '%_empl'` also valid syntax for quering prefixes/suffixes! – gavenkoa Feb 20 '15 at 00:23
  • if you're facing problem with nodejs then refere this https://stackoverflow.com/questions/34480683/get-list-of-tables-from-sqlite-in-node-js – Rishabh Agrawal Apr 24 '21 at 12:43

17 Answers17

1304

There are a few steps to see the tables in an SQLite database:

  1. List the tables in your database:

    .tables
    
  2. List how the table looks:

    .schema tablename
    
  3. Print the entire table:

    SELECT * FROM tablename;
    
  4. List all of the available SQLite prompt commands:

    .help
    
Tuan
  • 2,883
  • 4
  • 32
  • 54
Mark Janssen
  • 13,525
  • 2
  • 14
  • 4
  • 47
    `.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). – dbn Feb 06 '13 at 01:26
  • 32
    (This should be the accepted answer, it is the most sqlite-y way to do things). – dbn Feb 06 '13 at 01:27
  • 8
    `.tables` won't display tables if one opened database(s) through `ATTACH '' AS ;`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. – antiplex Mar 28 '13 at 21:33
  • 2
    @dbw: Not necesserily. Consider you are doing a DB wrapper able to use SQLite or MySql (my case). Using more SQL-conform commands would make it easier to port the wrapped in other languages then if you'd use DB-vendor specific commands. – Valentin Heinitz Apr 08 '13 at 08:42
  • "...sqlite3 will accept any command that is unambiguous..." Unfortunately sometimes it accepts ambiguous commands as well. For example, ".s" is interpreted as ".show", even though ".schema", '.separator", or ".stats" would be possibilities as well. And when it doesn't accept an ambiguous command, it doesn't list the possibilities. –  Aug 13 '14 at 17:48
  • @dbw hell, his answer could just be: `.tables` – stagl Mar 20 '16 at 23:33
  • 1
    This answer is missing context, so it's not helpful. – NL23codes Jun 22 '20 at 14:49
629

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';
Anthony Williams
  • 62,015
  • 12
  • 122
  • 149
  • 144
    Only `"SELECT name FROM sqlite_master WHERE type='table'"` works for me – vladkras Dec 15 '15 at 13:28
  • 5
    SELECT name FROM my_db.sqlite_master WHERE type='table'; this does not work for me (for the attached DB) and it throws error as: no such table exist "my_db.sqlite_master" – kanika Jul 27 '16 at 07:16
  • what you meant by temporary tables? Are there any when I just opened SQLite db file? – Ewoks May 07 '17 at 13:20
  • Temporary tables are those created with `CREATE TEMPORARY TABLE` SQL commands. Their contents are dropped when the current database connection is closed, and they are never saved to a database file. – Anthony Williams May 08 '17 at 14:37
  • 1
    Under sqlite3 command mode and run `ATTACH "some_file.db" AS my_db;` It worked! – John_J Dec 25 '17 at 07:22
  • You can also use ```"SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%';"``` to filter out system tables managed internally by SQLite. – Elder Druid Sep 11 '20 at 22:47
448

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.

ann
  • 584
  • 1
  • 9
  • 19
Lasse V. Karlsen
  • 350,178
  • 94
  • 582
  • 779
  • 121
    Not something easy to read or remember for use in the future; the builtin `.tables` command is more intuitive –  Feb 23 '13 at 22:02
  • 25
    @Gryllida: despite this is usable from any SQL-API as it's valide SQL. Built-in commands may not be supported everywhere. – Valentin Heinitz Apr 08 '13 at 08:36
  • 2
    @DoktorJ Have `.tables` been modified to display tables from an attached database? – Lasse V. Karlsen Nov 26 '13 at 19:15
  • Well if I start sqlite via `sqlite3 ./mydatabase.db` and then type `.tables`, I get a list of tables in that database... so yes. Similarly, `.schema mytable` shows me the table schema in a CREATE TABLE statement, and `SELECT * FROM mytable` shows me all the data in "mytable". – Doktor J Nov 26 '13 at 23:24
  • 5
    *In that database*, yes, but this question was about showing the tables in a database you have attached. Have the `.tables` command been modified to show those as well? – Lasse V. Karlsen Nov 27 '13 at 08:32
  • 4
    Doh! Reading comprehension fail... I somehow managed to not catch the ATTACH reference... twice >_ – Doktor J Nov 27 '13 at 16:49
  • you're just reading from `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 table – Honey Apr 26 '16 at 20:53
171

To 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?

Christian Davén
  • 13,902
  • 10
  • 53
  • 68
  • 17
    Thanks for the only answer that really addressed the question... "What SQL", not what command can be used... thanks! – Brad Parks Jan 18 '14 at 01:37
  • Also, this prints one table name per line, while .tables prints multiple columns of table names (annoying/not useful). – Shane Mar 21 '14 at 19:14
71

Use .help to check for available commands.

.table

This command would show all tables under your current database.

ann
  • 584
  • 1
  • 9
  • 19
Antony.H
  • 915
  • 9
  • 9
45

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
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
39

To list the tables you can also do:

SELECT name FROM sqlite_master
WHERE type='table';
Eugene Yarmash
  • 119,667
  • 33
  • 277
  • 336
Rafał Dowgird
  • 38,640
  • 11
  • 73
  • 89
  • So... `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. – jbuddy_13 Apr 23 '20 at 15:32
34

Try PRAGMA table_info(table-name);
http://www.sqlite.org/pragma.html#schema

Eugene Yarmash
  • 119,667
  • 33
  • 277
  • 336
Luiz Geron
  • 1,289
  • 15
  • 20
30

I use this query to get it:

SELECT name FROM sqlite_master WHERE type='table'

And to use in iOS:

NSString *aStrQuery=[NSString stringWithFormat:@"SELECT name FROM sqlite_master WHERE type='table'"];
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
GameLoading
  • 6,552
  • 2
  • 31
  • 57
18

According to the documentation, the equivalent of MySQLs' 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 answer.

Community
  • 1
  • 1
Alix Axel
  • 141,486
  • 84
  • 375
  • 483
16

As of the latest versions of SQLite 3 you can issue:

.fullschema

to see all of your create statements.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
pepper
  • 1,881
  • 4
  • 19
  • 28
  • SQLite version 3.7.13 2012-07-17 17:46:21 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite> .fullschema Error: unknown command or invalid arguments: "fullschema". Enter ".help" for help – Mona Jalal Oct 14 '14 at 19:06
  • 2
    You are using a version from 2012 – pepper Oct 14 '14 at 19:08
12

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... (assume your OS command line prompt is $) instead of $sqlite3:

sqlite3> ATTACH database.sqlite as "attached"

From your OS command line, open the database directly:

$sqlite3 database.sqlite
sqlite3> .dump
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Noah
  • 14,450
  • 11
  • 98
  • 147
11

Via a union all, combine all tables into one list.

select name
from sqlite_master 
where type='table'

union all 

select name 
from sqlite_temp_master 
where type='table'
openwonk
  • 10,827
  • 4
  • 31
  • 31
10

Use:

import sqlite3

TABLE_LIST_QUERY = "SELECT * FROM sqlite_master where type='table'"
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Mrityunjay Singh
  • 427
  • 5
  • 11
8

Since nobody has mentioned about the official reference of SQLite, I think it may be useful to refer to it under this heading:

https://www.sqlite.org/cli.html

You can manipulate your database using the commands described in this link. Besides, if you are using Windows OS and do not know where the command shell is, that is in the SQLite's site:

https://www.sqlite.org/download.html

After downloading it, click 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 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.

oiyio
  • 3,108
  • 3
  • 34
  • 41
6

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);
2

.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'