SQLite - Datatypes

By xngo on June 9, 2019

SQLite datatypes

Unlike most SQL database engines, SQLite has simple and shorter list of datatypes. They are:

  • 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(binary large object) of data, stored exactly as it was input.

Boolean Datatype

SQLite doesn't have a specific datatype for boolean value. Instead, use integers 0 for false and 1 for true.

Date and Time Datatype

SQLite doesn't have a specific datatype for dates and/or times. Instead, use the following datatypes to store dates and/or times:

  • TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
  • REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
  • INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.

SQLite provides built-in Date And Time Functions to manipulate dates and times.

Examples

CREATE TABLE images(  id INTEGER PRIMARY KEY AUTOINCREMENT,
                    DATE TEXT,
                    name TEXT,
                    SIZE INTEGER,
                    file BLOB
 
                    );
 
INSERT INTO images(DATE, name, SIZE, file) VALUES('2019-01-11', 'logo',   352, readfile('t1.png'));
INSERT INTO images(DATE, name, SIZE, file) VALUES('2018-01-11', 'logo1',  552, readfile('t1.png'));
INSERT INTO images(DATE, name, SIZE, file) VALUES('2015-01-11', 'logo3', 4552, readfile('t1.png'));
INSERT INTO images(DATE, name, SIZE, file) VALUES('2014-01-11', 'logo3', 1552, readfile('t1.png'));
 
SELECT * FROM images WHERE DATE >'2014-11-20' AND DATE < '2017-01-01';

Output

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.