AngryHacker
AngryHacker

Reputation: 61666

How can I get the list of a columns in a table for a SQLite database?

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

Upvotes: 150

Views: 155785

Answers (9)

nuwancy
nuwancy

Reputation: 391

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.

Upvotes: 5

Lewis Nakao
Lewis Nakao

Reputation: 7372

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'

Upvotes: 16

David Garoutte
David Garoutte

Reputation: 371

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
;

Upvotes: 32

Choca Croc
Choca Croc

Reputation: 11

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

Upvotes: 1

some ideas
some ideas

Reputation: 74

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.

Upvotes: 3

inVader
inVader

Reputation: 1534

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.

Upvotes: 37

Majd Taby
Majd Taby

Reputation: 1370

just go into your sqlite shell:

$ sqlite3 path/to/db.sqlite3

and then just hit

sqlite> .schema

and you will get everything.

Upvotes: 17

ifightcrime
ifightcrime

Reputation: 1302

Here's the simple way:

.schema <table>

Upvotes: 81

Bryan Kyle
Bryan Kyle

Reputation: 13771

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.

Upvotes: 186

Related Questions