ColumnInfo

A column of a database table.

This type closely matches the information returned by the table_info and table_xinfo pragmas.

sqlite> CREATE TABLE player (
   ...>   id INTEGER PRIMARY KEY,
   ...>   firstName TEXT,
   ...>   lastName TEXT);
sqlite> PRAGMA table_info(player);
cid     name        type        notnull     dflt_value  pk
------  ----------  ----------  ----------  ----------  -----
0       id          INTEGER     0                       1
1       firstName   TEXT        0                       0
2       lastName    TEXT        0                       0
sqlite> PRAGMA table_xinfo(player);
cid     name        type        notnull     dflt_value  pk     hidden
------  ----------  ----------  ----------  ----------  -----  ----------
0       id          INTEGER     0                       1      0
1       firstName   TEXT        0                       0      0
2       lastName    TEXT        0                       0      0

See Database.columns(in:) and https://www.sqlite.org/pragma.html#pragma_table_info

  • The column name

  • The column data type

    The casing of this string depends on the SQLite version: make sure you process this string in a case-insensitive way.

  • True if and only if the column is constrained to be not null.

  • The SQL snippet that defines the default value, if any.

    When nil, the column has no default value.

    When not nil, it contains an SQL string that defines an expression. That expression may be a literal, as 1, or 'foo'. It may also contain a non-constant expression such as CURRENT_TIMESTAMP.

    For more information, see https://www.sqlite.org/lang_createtable.html#the_default_clause.

    For example:

    try db.execute(sql: """
        CREATE TABLE player(
            id INTEGER PRIMARY KEY,
            name TEXT DEFAULT 'Anonymous',
            score INT DEFAULT 0,
            creationDate DATE DEFAULT CURRENT_TIMESTAMP
        )
        """)
    let columnInfos = try db.columns(in: "player")
    columnInfos[0].defaultValueSQL // nil
    columnInfos[1].defaultValueSQL // "'Anonymous'"
    columnInfos[2].defaultValueSQL // "0"
    columnInfos[3].defaultValueSQL // "CURRENT_TIMESTAMP"
    
  • For columns that are part of the primary key, this is the one-based index of the column in the primary key. For other columns, it is zero.