Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

sql.sqlite.types

import sql.sqlite.types

sql lifts part of this module out to its own top level; each name below is shown with the path that reaches it. Anything still spelled sql.sqlite.types.* needs import sql.sqlite.types.

Reading SQLite values back as the types a column was declared to hold.

SQLite stores five things and remembers nothing about intent. A boolean goes in as 1 and comes back as the number 1; a timestamp goes in as text and comes back as text. What it does keep is the type each column was declared with, and that is enough to put the value back the way it went in.

So decoding here is driven by the declaration and never by the value. A column declared BOOLEAN yields true and false; one declared INTEGER yields the number 1 even when every row in it happens to be 0 or 1. Guessing from content would make the type of a result depend on the rows it happened to return, which is the kind of thing that works until the day a table has different data in it.

A column with no declared type, which is what an expression has, comes back exactly as SQLite stored it.

Constants

BOOLEAN_TYPES

sql.sqlite.types.BOOLEAN_TYPES = [...]

Declared types that mean a boolean.

TEMPORAL_TYPES

sql.sqlite.types.TEMPORAL_TYPES = [...]

Declared types that mean a point in time.

JSON_TYPES

sql.sqlite.types.JSON_TYPES = [...]

Declared types that mean JSON held in a text column.

Functions

base_type()

sql.sqlite.base_type(declared) -> string

The bare type name from a declaration, upper cased and without any size or precision.

VARCHAR(255) is VARCHAR, decimal(10, 2) is DECIMAL, and an undeclared column is the empty string.

Parameters

  • declared (string|nil)

Returns string

conversion_for()

sql.sqlite.conversion_for(declared) -> string|nil

Which conversion a column’s declaration calls for: 'bool', 'date', 'json', or nil for a column that needs none.

Parameters

  • declared (string|nil) — The column’s declared type.

Returns string|nil

decode()

sql.sqlite.types.decode(value, conversion) -> any

Applies one column’s conversion to one stored value.

NULL stays nil whatever the column was declared as. A value that will not convert, such as text in a DATETIME column that is not a timestamp, comes back as it was stored rather than as nil: losing the value would be a worse answer than handing back the text that is actually in the database.

Parameters

  • value (any) — The value as SQLite stored it.
  • conversion (string|nil) — From conversion_for().

Returns any

conversions_for()

sql.sqlite.types.conversions_for(declared: list) -> list

The conversions for a whole result, one per column, worked out once so each row does not have to look at the declarations again.

Parameters

  • declared (list) — The declared types, in column order.

Returns list

decode_row()

sql.sqlite.types.decode_row(values: list, conversions: list) -> list

Applies a result’s conversions to one row of stored values.

Parameters

  • values (list)
  • conversions (list)

Returns list


2026, Richard Ore and Zuri contributors