SQLite Data Types
SQLite data type is an attribute used to specify the data type of any object. Every column, variable, and expression in SQLite has an associated data type.
You can use these data types when creating tables. SQLite uses a more general dynamic type system. In SQLite, the data type of a value is associated with the value itself, not with its container.
SQLite Storage Classes
Every value stored in an SQLite database has one of the following storage classes:
| Storage Class | Description |
|---|---|
| NULL | The value is a NULL value. |
| INTEGER | The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value. |
| REAL | The value is a floating-point value, stored as an 8-byte IEEE floating-point number. |
| TEXT | The value is a text string, stored using the database encoding (UTF-8, UTF-16BE, or UTF-16LE). |
| BLOB | The value is a blob of data, stored exactly as it was input. |
SQLite's storage classes are somewhat more general than data types. The INTEGER storage class, for example, includes 6 different integer data types of varying lengths.
SQLite Affinity Types
SQLite supports the concept of column affinity. Any column can still store data of any type; when data is inserted, the field will preferentially use the affinity type as the storage method for that value. Current versions of SQLite support the following five affinity types:
| Affinity Type | Description |
|---|---|
| TEXT | Numeric data must first be converted to text format before being inserted, and then inserted into the target field. |
| NUMERIC | When text data is inserted into a field with NUMERIC affinity, if the conversion does not cause any loss of data information and is completely reversible, SQLite converts the text data to INTEGER or REAL; if the conversion fails, SQLite still stores the data as TEXT. For new data of NULL or BLOB type, SQLite makes no conversion and stores it directly as NULL or BLOB. It should be noted that for constant text in floating-point format, such as "30000.0", if the value can be converted to INTEGER without losing numeric information, SQLite will convert it to INTEGER storage. |
| INTEGER | For a field with INTEGER affinity, the rules are the same as for NUMERIC, with the only difference occurring when executing a CAST expression. |
| REAL | Its rules are basically the same as NUMERIC, with the only difference being that text data like "30000.0" is not converted to INTEGER storage. |
| NONE | No conversion is performed; the data is stored directly in its own data type. |
SQLite Affinity Types and Type Names
The following table lists the various data type names that can be used when creating an SQLite3 table, and also shows the corresponding affinity types:
| Data Type | Affinity Type |
|---|---|
|
|
INT
INTEGER
TINYINT
SMALLINT
MEDIUMINT
BIGINT
UNSIGNED BIG INT
INT2
INT8
-
CHARACTER(20)
-
VARCHAR(255)
-
VARYING CHARACTER(255)
-
NCHAR(55)
-
NATIVE CHARACTER(70)
-
NVARCHAR(100)
-
TEXT
-
CLOB
-
BLOB
-
Unspecified Type
-
REAL
-
DOUBLE
-
DOUBLE PRECISION
-
FLOAT
-
NUMERIC
-
DECIMAL(10,5)
-
BOOLEAN
-
DATE
-
DATETIME
Boolean Data Type
SQLite has no separate Boolean storage class. Instead, Boolean values are stored as the integers 0 (false) and 1 (true).
Date and Time Data Types
SQLite has no separate storage class for storing dates and/or times, but it can store dates and times as TEXT, REAL, or INTEGER values.
| Storage Class | Date format |
|---|---|
| TEXT | Dates in the format "YYYY-MM-DD HH:MM:SS.SSS". |
| REAL | The number of days since noon on November 24, 4714 BC Greenwich Mean Time. |
| INTEGER | The number of seconds since 1970-01-01 00:00:00 UTC. |
You can store dates and times in any of the above formats, and you can use the built-in date and time functions to freely convert between formats.