SQLite Data Types
Storage Classes
Every value in SQLite has one of five storage classes:
| Storage class | Holds | Example |
|---|---|---|
| NULL | No value | NULL |
| INTEGER | Whole numbers | 120 |
| REAL | Decimal numbers | 9.5 |
| TEXT | Text | 'Berlin' |
| BLOB | Data exactly as it was given, like an image | X'48656C6C6F' |
The typeof() function returns the storage class of a value:
Example
SELECT typeof(120), typeof(9.5), typeof('Berlin'),
typeof(NULL), typeof(X'48656C6C6F');
Try it Yourself »
Flexible Types
Most databases only let you store one type of data in a column. SQLite is more flexible. A column type is only a preference, called the type affinity.
In this example, the Amount column is an INTEGER column. The text '20' looks like a number, so SQLite turns it into the integer 20. The text 'abc' does not look like a number, so it is stored as text:
Example
CREATE TABLE IF NOT EXISTS TypeTest (
Amount INTEGER
);
INSERT INTO TypeTest VALUES (10), ('20'), ('abc');
SELECT Amount, typeof(Amount)
FROM TypeTest;
Try it Yourself »
Tip: If you want SQLite to check the types, use a STRICT table.
Type Affinity
SQLite looks at the type name you write for a column, and gives the column one of five affinities:
| The type name | Affinity | Examples |
|---|---|---|
| contains INT | INTEGER | INT, INTEGER, BIGINT |
| contains CHAR, CLOB or TEXT | TEXT | TEXT, VARCHAR(255), NVARCHAR(100) |
| contains BLOB, or no type | BLOB | BLOB |
| contains REAL, FLOA or DOUB | REAL | REAL, DOUBLE, FLOAT |
| anything else | NUMERIC | NUMERIC, DECIMAL(10,2), BOOLEAN, DATE |
This is why SQL from other databases, like VARCHAR(255), also works in SQLite.
Boolean Values
SQLite has no boolean type. True and false are stored as the integers 1 and 0. The keywords TRUE and FALSE are just other names for 1 and 0:
Dates and Times
SQLite has no date type either. Dates are stored as text, like '1996-07-04', or as numbers. Read more in the SQLite Dates chapter.