135

I am looking to retrieve a list of columns in a table. The database is the latest release of SQLite (3.6, I believe). I am looking for code that does this with a SQL query. Extra bonus points for metadata related to the columns (e.g. length, data type, etc...)

AngryHacker
  • 59,598
  • 102
  • 325
  • 594

9 Answers9

173

What you're looking for is called the data dictionary. In sqlite a list of all tables can be found by querying sqlite_master table (or view?)

sqlite> create table people (first_name varchar, last_name varchar, email_address varchar);
sqlite> select * from sqlite_master;
table|people|people|2|CREATE TABLE people (first_name varchar, last_name varchar, email_address varchar)

To get column information you can use the pragma table_info(table_name) statement:

sqlite> pragma table_info(people);
0|first_name|varchar|0||0
1|last_name|varchar|0||0
2|email_address|varchar|0||0

For more information on the pragma statements, see the documentation.

Bryan Kyle
  • 13,361
  • 4
  • 40
  • 45
  • 7
    Great! Now how is this done from outside the command line? How is this done from within my own C program? – Aaron Bratcher Sep 27 '13 at 13:56
  • how can i implement same as above in Objective-c – Nag Raj May 20 '14 at 09:27
  • 2
    @Nag, I would think you that SQLite should just treat these commands as ordinary SQL, process it accordingly, and return you a result set. – Bryan Kyle May 30 '14 at 15:06
  • doesn't sqlite have some dot-shortcut instead of `select * from `? – jiggunjer Aug 02 '16 at 06:34
  • 1
    Using select * from table, when you don't know how much records in the table might have result of milions records and take time and recources. You should add "limit 1" or something like it. – Guy Dafny Nov 25 '19 at 08:24
74

Here's the simple way:

.schema <table>
ifightcrime
  • 1,216
  • 15
  • 18
34

The question is old but the following hasn't been mentioned yet.

Another convenient way in many cases is to turn headers on by:

sqlite> .headers on

Then,

sqlite> SELECT ... FROM table

will display a headline showing all selected fields (all if you SELECT *) at the top of the output.

inVader
  • 1,504
  • 14
  • 25
  • I my guess is correct that the actual problem was that sqlite3 does not provide the header of a query by default, then **that is the solution we all have been looking for**. If your table is too long to be displayed on your terminal, just add, e.g. `LIMIT 5`. Do not forget the `;` at the end. – fralau Mar 24 '19 at 07:31
28

Here's a SELECT statement that lists all tables and columns in the current database:

SELECT m.name as tableName, 
       p.name as columnName
FROM sqlite_master m
left outer join pragma_table_info((m.name)) p
     on m.name <> p.name
order by tableName, columnName
;
David Garoutte
  • 331
  • 4
  • 4
16

just go into your sqlite shell:

$ sqlite3 path/to/db.sqlite3

and then just hit

sqlite> .schema

and you will get everything.

Java Man
  • 1,854
  • 3
  • 21
  • 43
Majd Taby
  • 1,340
  • 1
  • 11
  • 15
15

This is a query that lists all tables with their columns, and all the metadata I could get about each column as OP requested (as bonus points).

SELECT
  m.name AS table_name, 
  p.cid AS col_id,
  p.name AS col_name,
  p.type AS col_type,
  p.pk AS col_is_pk,
  p.dflt_value AS col_default_val,
  p.[notnull] AS col_is_not_null
FROM sqlite_master m
LEFT OUTER JOIN pragma_table_info((m.name)) p
  ON m.name <> p.name
WHERE m.type = 'table'
ORDER BY table_name, col_id

Thanks to @David Garoutte for showing me how to get pragma_table_info to work in a query.

Run this query to see all the table metadata:

SELECT * FROM sqlite_master WHERE type = 'table'
Erica
  • 2,399
  • 5
  • 26
  • 34
Lewis Nakao
  • 7,079
  • 2
  • 26
  • 21
3

Building on the above, you can do it all at once:

sqlite3 yourdb.db ".schema"

That will give you the SQL to create the table, which is effectively a list of the columns.

some ideas
  • 64
  • 3
  • 14
2

In case if you want to get all column names into one single comma separated string, you can use below.

SELECT GROUP_CONCAT(NAME,',') FROM PRAGMA_TABLE_INFO('table_name')

Here the pragma table_info is used as pragma_table_info for the select statement and GROUP_CONCAT is to combine all the field names into one string. for the second parameter of GROUP_CONCAT you can pass the separator.

nuwancy
  • 361
  • 4
  • 4
0

I know, it’s been a long time but it’s never too late… I had a similar question with TCL as interpreter and after several search, found nothing good for me. So I propose something based on PRAGMA, knowing that your DB is “main”

db eval { PRAGMA main.table_info(<your table name>) } TBL { puts $TBL(name) }

And array use to obtain a list

set col_list {}
db eval { PRAGMA main.table_info(<your table name>) } TBL { lappend col_list $TBL(name) }
puts $col_list