How to Store True and False in SQLite
Is a task done or not? SQLite has no true-or-false type: it stores 1 for true and 0 for false. This guide saves two tasks with TRUE and FALSE and finds the one that's done, with real output from SQLite 3.54.1.
Syntax
CREATE TABLE <table> (<column> INTEGER NOT NULL);
INSERT INTO <table> (<column>) VALUES (TRUE);
SELECT <columns> FROM <table> WHERE <column>;
| Part | What it means |
|---|---|
<column> INTEGER | The column holds the true-or-false value as a number |
TRUE, FALSE | Other names for 1 and 0. SQLite stores the numbers |
WHERE <column> | Keeps the rows where the column is true, which means any number but 0 |
SQLite's documentation on Booleans: "SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored as integers 0 (false) and 1 (true)."
Prerequisites
- The sqlite3 command-line shell. This guide uses SQLite 3.54.1. Run
sqlite3 --versionto see yours; the SQLite download page has the command-line tools for Windows, macOS and Linux.
Step 1: Write the Script
Save this as true_false.sql:
CREATE TABLE tasks (
title TEXT NOT NULL,
done INTEGER NOT NULL
);
INSERT INTO tasks (title, done) VALUES ('Buy milk', TRUE);
INSERT INTO tasks (title, done) VALUES ('Pay rent', FALSE);
SELECT title, done, typeof(done) FROM tasks;
SELECT title FROM tasks WHERE done;

done INTEGER NOT NULLholds the true-or-false value as a number.- Buy milk is saved with
TRUE, and Pay rent withFALSE. typeof(done)shows the type SQLite stored.WHERE donekeeps the tasks whosedoneis true.
Step 2: Run It
In the folder that holds the script, run:
sqlite3 -table todo.db ".read true_false.sql"
sqlite3 opens the database file todo.db, and creates it when it isn't there. -table prints each result as a table, and .read runs the statements in the script. The command is the same in bash, zsh and PowerShell.
+----------+------+--------------+
| title | done | typeof(done) |
+----------+------+--------------+
| Buy milk | 1 | integer |
| Pay rent | 0 | integer |
+----------+------+--------------+
+----------+
| title |
+----------+
| Buy milk |
+----------+

TRUE was stored as 1 and FALSE as 0, both integers. WHERE done kept Buy milk, the task that's done.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Writing 'true' in quotes | The text true was stored, of type text, and WHERE done didn't match it | Write TRUE or 1, without quotes |
Testing with = TRUE | A done of 2 matched WHERE done, but not done = TRUE, because 2 isn't 1 | Use WHERE done, or done IS TRUE, which matched |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| A value other than 0 or 1 got into the column | Nothing limits the column to 0 and 1 | Add CHECK (done IN (0, 1)), which refused 2 and 'true' with CHECK constraint failed: done IN (0, 1) |
Parse error near line 1 of true_false.sql: table tasks already exists | The script already ran on todo.db | Delete todo.db and run the script again |
FAQ
How do I find the tasks that aren't done?
With NOT: SELECT title FROM tasks WHERE NOT done; returned Pay rent.
Since when does SQLite know TRUE and FALSE?
Since version 3.23.0. The documentation: "SQLite recognizes the keywords "TRUE" and "FALSE", as of version 3.23.0 (2018-04-02) but those keywords are really just alternative spellings for the integer literals 1 and 0 respectively."
Conclusion
TRUE and FALSE stored 1 and 0 in an INTEGER column, and WHERE done found the task that was done.
Additional Resources
- Boolean datatype — how SQLite stores true and false.
- Datatypes In SQLite — every storage class.
