SQLite Schema Information Metadata

后端 未结 7 1045
终归单人心
终归单人心 2020-11-27 03:46

I need to get column names and their tables in a SQLite database. What I need is a resultset with 2 columns: table_name | column_name.

In MySQL, I\'m a

7条回答
  •  醉酒成梦
    2020-11-27 04:11

    Recent versions of SQLite allow you to select against PRAGMA results now, which makes this easy:

    SELECT 
      m.name as table_name, 
      p.name as column_name
    FROM 
      sqlite_master AS m
    JOIN 
      pragma_table_info(m.name) AS p
    ORDER BY 
      m.name, 
      p.cid
    

    where p.cid holds the column order of the CREATE TABLE statement, zero-indexed.

    David Garoutte answered this here, but this SQL should execute faster, and columns are ordered by the schema, not alphabetically.

    Note that table_info also contains

    • type (the datatype, like integer or text),
    • notnull (1 if the column has a NOT NULL constraint)
    • dflt_value (NULL if no default value)
    • pk (1 if the column is the table's primary key, else 0)

    RTFM: https://www.sqlite.org/pragma.html#pragma_table_info

提交回复
热议问题