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

2019-01-08 08:46发布

问题:

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

回答1:

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.



回答2:

Here's the simple way:

.schema <table>


回答3:

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.



回答4:

just go into your sqlite shell:

$ sqlite3 path/to/db.sqlite3

and then just hit

sqlite> .schema

and you will get everything.



回答5:

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
;


回答6:

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.



回答7:

This is a query that lists all tables with their columns and all the meta data 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 * sqlite_master WHERE type = 'table'


回答8:

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