I know about the boolean
column type, but is there a boolean
literal in SQLite? In other languages, this might be true
There is no boolean data type. There are only 5 types, listed here. Integers can be stored with various widths on disk, the smallest being 1 byte. However, this is an implementation detail:
"But as soon as INTEGER values are read off of disk and into memory for processing, they are converted to the most general datatype (8-byte signed integer)."
Given that, it is not surprising there are no boolean literals.
SQLite has TRUE and FALSE literal as shown here :
https://www.sqlite.org/syntax/literal-value.html
(yeah, long time to respond :)
BOOLEAN -> NUMERIC (Affinity)
Column Affinity
SQLite supports the concept of type affinity on columns. Any column can still store any type of data but the preferred storage class for a column is called its affinity. Each table column in an SQLite3 database is assigned one of the following type affinities: Affinity Description
Boolean Datatype:
SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored as integers 0 (false) and 1 (true).
@moiing-duck "On the flip side, SQLite supports datetimes, despite not being one of those five types, so this answer is inconclusive"
SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:
From section 1.1 Boolean Datatype of the docs:
SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored as integers 0 (false) and 1 (true).
So it looks like you are stuck with 0
and 1
.
You can use BOOLEAN
when creating a table:
sqlite3 :memory:
SQLite version 3.22.0 2018-01-22 18:45:57
Enter ".help" for usage hints.
sqlite> CREATE TABLE test (mybool BOOLEAN);
But this is not a real datatype and on the SQL description of the table we will have something like this:
"mybool" BOOLEAN NOT NULL CHECK(mybool IN(0,1))
Basically we create an SQL constrain constrain that the value should be 0 or 1. And therefore using TRUE/FALSE will pop an error:
sqlite> INSERT INTO test(mybool) VALUES (TRUE);
Error: no such column: TRUE
So, we can use the key word BOOLEAN
but have to use 0/1 since it is not a "real" datatype.
When working with sqlalchemy we can use the datatype BOOLEAN without any problems
Is there a boolean literal in SQLite?
As stated in Justin Ethier's answer, SQLite does not have specific data type for boolean. But starting from SQLite 3.23.0 it supports true/false literals:
Recognize TRUE and FALSE as constants. (For compatibility, if there exist columns named "true" or "false", then the identifiers refer to the columns rather than Boolean constants.)
Support operators IS TRUE, IS FALSE, IS NOT TRUE, and IS NOT FALSE.
SELECT true AS t, false AS f;
SELECT 'condition is true'
WHERE 1 IS NOT FALSE;
CREATE TABLE a (id INT, b BOOLEAN DEFAULT(TRUE));
INSERT INTO a(id) VALUES(100);
SELECT * FROM a;
-- id b
-- 100 1
SELECT * FROM a WHERE true;
-- id b
-- 100 1
dbfiddle.com demo