I know about the boolean
column type, but is there a boolean
literal in SQLite? In other languages, this might be true
As everyone else has pointed out, SQLite does not support a specific boolean storage type, and the OP specifically acknowledges this fact. However, this is completely independent of whether SQLite supports boolean literals and comprehensions.
For anyone wondering, the answer is YES, since SQLite 3.23 you can do boolean comprehensions with the boolean literals TRUE
and FALSE
, and it will work how you expect.
For example, you can do:
SELECT * FROM blah WHERE some_column IS FALSE;
SELECT * FROM blah WHERE some_column IS TRUE;
and it will work how you expect if you are using 0
for false and 1
for true.
From my testing, here is how SQLite matches various values:
IS TRUE
IS FALSE
IS NULL
. Does not match IS TRUE
or IS FALSE
.IS FALSE
. Even values like "t", "TRUE", "true", "True" still match IS FALSE
The question is explicitly not about the column type (i.e storage-wise) but the use of TRUE
and FALSE
literals (i.e. parser-wise), which are SQL-compliant as per the PostgreSQL keywords documentation (which happens to also include SQL-92, SQL:2008 and SQL:2011 columns in the reference table).
The SQLite documentation lists all supported keywords, and this list contains neither TRUE
nor FALSE
, hence SQLite sadly is non-compliant in that regard.
You can also test it easily and see how the parser barfs as it wants the token to be a column name:
$ sqlite3 :memory:
SQLite version 3.14.0 2016-07-26 15:17:14
sqlite> CREATE TABLE foo (booleanish INT);
sqlite> INSERT INTO foo (booleanish) VALUES (TRUE);
Error: no such column: TRUE
SQLite doesn't have Boolean type, you should use INTEGER with 0 is false and 1 is true
1.1 Boolean Datatype
SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored as integers 0 (false) and 1 (true).
Docs
I noticed in sqlite for android, I can declare a Boolean column type with no error and its seems to work fine. I also tried defining the column as 'int' and storing java boolean values. I downloaded the db and confirmed I'm writing "true" in the column. I think it just works.
There are only 5 datatypes supported in SQLite3.
From the Official SQLite3 doc. "Each value stored in an SQLite database (or manipulated by the database engine) has one of the following storage classes:
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."
If you are going to store 1s and 0s, then SQLite wil use 1 byte if storage. Which is not bad. Official Doc link :- http://www.sqlite.org/datatype3.html