Skip to main content

How to Store True and False in SQLite

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
CREATE TABLE <table> (<column> INTEGER NOT NULL);

INSERT INTO <table> (<column>) VALUES (TRUE);
SELECT <columns> FROM <table> WHERE <column>;
PartWhat it means
<column> INTEGERThe column holds the true-or-false value as a number
TRUE, FALSEOther 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 --version to 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:

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;

The true_false.sql script in a macOS-style code window: a tasks table with title and an INTEGER done column, Buy milk inserted with TRUE and Pay rent with FALSE, a SELECT of title, done and typeof(done), and a SELECT of the titles WHERE done.

  • done INTEGER NOT NULL holds the true-or-false value as a number.
  • Buy milk is saved with TRUE, and Pay rent with FALSE.
  • typeof(done) shows the type SQLite stored.
  • WHERE done keeps the tasks whose done is true.

Step 2: Run It​

In the folder that holds the script, run:

Terminal
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.

Output
+----------+------+--------------+
| title | done | typeof(done) |
+----------+------+--------------+
| Buy milk | 1 | integer |
| Pay rent | 0 | integer |
+----------+------+--------------+
+----------+
| title |
+----------+
| Buy milk |
+----------+

The output in a terminal: the tasks with Buy milk at done 1 and Pay rent at done 0, both of type integer, then a result holding only 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​

MistakeWhat happensDo this instead
Writing 'true' in quotesThe text true was stored, of type text, and WHERE done didn't match itWrite TRUE or 1, without quotes
Testing with = TRUEA done of 2 matched WHERE done, but not done = TRUE, because 2 isn't 1Use WHERE done, or done IS TRUE, which matched

Troubleshooting​

SymptomLikely causeFix
A value other than 0 or 1 got into the columnNothing limits the column to 0 and 1Add 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 existsThe script already ran on todo.dbDelete 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​