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

后端 未结 8 1153
梦谈多话
梦谈多话 2020-11-28 23:04

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

相关标签:
8条回答
  • 2020-11-28 23:48

    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.

    0 讨论(0)
  • 2020-11-28 23:48

    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.

    0 讨论(0)
提交回复
热议问题