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

后端 未结 8 1233
梦谈多话
梦谈多话 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:38

    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'
    

提交回复
热议问题