Skip to content

SQLite - Data Type

A key feature that distinguishes SQLite from most other SQL databases is its dynamic type system. In SQLite, the data type is a property of the value itself, not the column in which the value is stored. This provides great flexibility, but it’s important to understand the underlying concepts of Storage Classes and Type Affinity.

Every value stored in an SQLite database is assigned one of the following five storage classes:

Sr.No.Storage Class & Description
1NULL
The value is a NULL value.
2INTEGER
A signed integer (1, 2, 3, 4, 6, or 8 bytes), depending on the magnitude.
3REAL
A floating-point value, stored as an 8-byte IEEE floating-point number.
4TEXT
A text string, stored using the database encoding (e.g., UTF-8).
5BLOB (Binary Large Object)
A blob of data, stored exactly as it was input. Used for files, images, etc.

While any column can technically store any type of value, columns are assigned a “type affinity”. Affinity is a recommendation for the column’s preferred storage class. SQLite will try to coerce values into the column’s affinity type where it makes sense.

AffinityDescriptionTriggered by Type Names Containing
INTEGERPrefers storing data as an integer. Values that look like numbers but have decimal points might be stored as REAL.INT
TEXTPrefers storing data as text.CHAR, CLOB, TEXT
BLOBPrefers storing data as a BLOB. No conversions are attempted.BLOB (or no type specified)
REALPrefers storing data as a floating-point number.REAL, FLOA, DOUB
NUMERICA flexible affinity that can store values in any format. It will try to store data as INTEGER or REAL if possible, otherwise falls back to TEXT.NUMERIC, DECIMAL, BOOL, DATE

SQLite does not have a distinct Boolean storage class. It stores boolean values as INTEGER 0 (false) and 1 (true). Modern versions of SQLite recognize the keywords TRUE and FALSE in SQL statements, making queries more readable.

CREATE TABLE tasks (id INTEGER PRIMARY KEY, description TEXT, is_complete INTEGER);
-- Both statements below are equivalent
INSERT INTO tasks (description, is_complete) VALUES ('Write tutorial', 1);
INSERT INTO tasks (description, is_complete) VALUES ('Publish tutorial', TRUE);

As covered in the Date & Time chapter, SQLite handles dates by storing them as TEXT, REAL, or INTEGER and manipulating them with built-in functions. Declaring a column with DATE or DATETIME gives it NUMERIC affinity, but TEXT is often the most practical choice for storing ISO 8601 strings.

SQLite has excellent support for JSON data, which is stored in columns with TEXT affinity. A rich set of json_* functions allows you to query and manipulate JSON documents directly within your SQL.

CREATE TABLE products (
id INTEGER PRIMARY KEY,
name TEXT,
attributes TEXT -- Storing JSON data as text
);
INSERT INTO products (name, attributes) VALUES (
'Laptop',
'{"brand": "AwesomeCorp", "ram_gb": 16, "storage": ["512gb_ssd", "1tb_hdd"]}'
);
-- Extract a value from the JSON object using json_extract()
SELECT name, json_extract(attributes, '$.brand') AS brand
FROM products
WHERE json_extract(attributes, '$.ram_gb') >= 16;