84

I know about the boolean column type, but is there a boolean literal in SQLite? In other languages, this might be true or false. Obviously, I can use 0 and 1, but I tend to avoid so-called "magic numbers" where possible.

From this list, it seems like it might exist in other SQL implementations, but not SQLite. (I'm using SQLite 3.6.10, for what it's worth.)

Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
Benjamin Oakes
  • 12,262
  • 12
  • 65
  • 83

12 Answers12

102

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.

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
  • 12
    I came here *after* reading that exact bit of the documentation, which, IMHO, is extremely ambiguous about whether `0` and `1` are somehow aliased with the identifiers `false` and `true`, respectively, by SQLite's SQL parser. – O. R. Mapper May 07 '14 at 14:24
  • 7
    Sadly this answer doesn't respond to the question, which is not about the storage class/*column type* but the use of `TRUE` and `FALSE` *literals*, which are SQL-compliant as [per the Postgresql doc](https://www.postgresql.org/docs/8.1/static/datatype-boolean.html). – Lloeki Mar 01 '17 at 16:18
  • @Lloeki - Sadly, you referenced Postgresql documentation for a SQLite question... – Justin Ethier Mar 01 '17 at 18:38
  • 1
    Sorry, I meant *ANSI* SQL-compliant, but couldn't find a definitive reference quickly enough so fell back to a note from the PostgreSQL doc. My comment started to turn like an answer so I cut short and transitioned into writing one that has the full run down and effectively gets to answer the actual question. – Lloeki Mar 02 '17 at 13:57
  • 1
    @Lloeki - SQLite is not ANSI SQL compliant with respect to booleans, though - their documentation is the definitive source. Along those lines, your link to the SQLite list of keywords is very useful to make it clear `TRUE` and `FALSE` are not supported. – Justin Ethier Mar 02 '17 at 15:05
  • 4
    @Lloeki SQLite now supports literals :) Please check https://stackoverflow.com/a/54223589/5070879 – Lukasz Szozda Jan 16 '19 at 18:59
32

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:

  1. 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.)

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

Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
10

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

Community
  • 1
  • 1
Andrey
  • 59,039
  • 12
  • 119
  • 163
  • 6
    I came here *after* reading that exact bit of the documentation, which, IMHO, is extremely ambiguous about whether `0` and `1` are somehow aliased with the identifiers `false` and `true`, respectively, by SQLite's SQL parser. – O. R. Mapper May 07 '14 at 14:24
  • @O.R.Mapper IMO false and true doesn't exist in world of SQLite. – Andrey May 07 '14 at 14:28
  • 5
    Yes, I figured so, based on the answers to this question, but the cited portion of the docs is extremely unclear about that IMO. It says boolean values are *stored* as the numbers `0` and `1`, respectively - and then indicates that those numbers match *false* and *true*. What are *false* and *true* there? Hints in natural language to readers, or constants/aliases understood by SQLite's SQL parser? As I said, it's very ambiguous the way it is written. – O. R. Mapper May 07 '14 at 14:29
  • @O.R.Mapper for me it is clear. They mean that 0 is equivalent to `false` value of boolean logic. – Andrey May 07 '14 at 14:30
  • @O.R.Mapper they don't match, they represent - this is what they meant. – Andrey May 07 '14 at 14:31
  • 5
    Doesn't say so. It could just as well be a `false` value understood by SQLite's SQL parser, it's just not clear. That's why reading that portion of the docs left me totally clueless and looking for some additional confirmation that there are no such constants `false` and `true` in SQLite, which is how I got here. – O. R. Mapper May 07 '14 at 14:32
  • @O.R.Mapper clearness is matter of personal perception, I can't convince you that is is clearly written, for me personally it was clear. My logic was that if there is no thing as boolean datatype so that if true/false exist they must be integer constants. 10 seconds test can demonstrate that they are not constants/keywords. I actually don't know any language without boolean type and with boolean constants. – Andrey May 07 '14 at 15:30
  • In some versions of C that do not have a boolean type, `define`-ing `TRUE` and `FALSE` [isn't unusual](http://stackoverflow.com/questions/17010041/why-define-true-1-1-in-a-c-boolean-macro-instead-of-simply-as-1). – O. R. Mapper May 07 '14 at 15:52
  • @O.R.Mapper C doesn't have bool by design. Some compilers may introduce pseudo-bool constants but it is bad idea imo, because no portability. Defining constant on your own is different thing from having it within language. – Andrey May 08 '14 at 11:07
  • 3
    Some C compilers may introduce pseudo-bool constants, just as some SQL parsers may introduce pseudo-bool constants. – O. R. Mapper May 08 '14 at 11:38
  • 1
    @Andrey C may not have an explicit boolean type, but it has booleans (i.e. whatever you put in an if condition, be it a comparison, integer, or result of a function call, is treated as a boolean because it can only have one of two effects), so it makes sense to have aliases to true and false. By the same token, SQLite doesn't have a boolean type, but it also has booleans, so it would make sense for a language designer to define aliases there, also. "Defining constant on your own is different thing from having it within language" Yes, but there are standard library defines. – Parthian Shot Mar 17 '15 at 19:25
  • @ParthianShot what you say can shorter be expressed as that SQLite supports boolean expressions. – Andrey Mar 17 '15 at 20:29
9

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
Lloeki
  • 6,573
  • 2
  • 33
  • 32
  • This is the correct answer. Sqlite client libraries sometimes suppress this error (WTF), but as you've shown, if you run it in the command-line `sqlite3` app, it doesn't like "true" or "false" literals. – Andrew Koster Mar 09 '20 at 16:57
  • This is no longer the correct answer as of SQLite version 3.23, as noted in Lukasz's answer. – AndyMcoy Jul 06 '23 at 17:40
4

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:

  • Any non-zero NUMERIC, INTEGER, or REAL: IS TRUE
  • Any zero NUMERIC, INTEGER, or REAL: IS FALSE
  • A null value: IS NULL. Does not match IS TRUE or IS FALSE.
  • Any TEXT that does not parse to a numeric value: IS FALSE. Even values like "t", "TRUE", "true", "True" still match IS FALSE
  • TEXT that looks like a number (eg "0", "1", "5"): Behaves like NUMERIC, listed above.
  • BLOB: Untested.
Ryan
  • 1,670
  • 18
  • 25
0

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.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • 2
    On the flip side, SQLite supports datetimes, despite not being one of those five types, so this answer is inconclusive. https://www.sqlite.org/datatype3.html#datetime – Mooing Duck Dec 17 '15 at 21:42
0

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.

0

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

Jorge Mendes
  • 388
  • 3
  • 14
-1

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

Akshay Patil
  • 954
  • 1
  • 12
  • 28
  • On the flip side, SQLite supports datetimes, despite not being one of those five types, so this answer is inconclusive. sqlite.org/datatype3.html#datetime – Mooing Duck Dec 17 '15 at 21:43
-1

SQLite has TRUE and FALSE literal as shown here :

https://www.sqlite.org/syntax/literal-value.html

(yeah, long time to respond :)

HugO
  • 29
  • 2
-2

SQLite doesn't have Boolean type, you should use INTEGER with 0 is false and 1 is true

-3

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

  • TEXT This column stores all data using storage classes NULL, TEXT or BLOB.
  • NUMERIC This column may contain values using all five storage classes.
  • INTEGER Behaves the same as a column with NUMERIC affinity with an exception in a CAST expression.
  • REAL Behaves like a column with NUMERIC affinity except that it forces integer values into floating point representation
  • NONE A column with affinity NONE does not prefer one storage class over another and no attempt is made to coerce data from one storage class into another.

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:

ceph3us
  • 7,326
  • 3
  • 36
  • 43
  • On the flip side, SQLite supports datetimes, despite not being one of those five types, so this answer is inconclusive. sqlite.org/datatype3.html#datetime – Mooing Duck Dec 17 '15 at 21:43
  • 3
    Well, that was aggressive. Since you completely misunderstood my point, I'll clarify: The OP is not asking if sqlite has a boolean column affinity. The OP is asking if Sqlite understands 'true' and 'false'. The fact that sqlite has built in functions for dates and times is proof that a focus on affinity is irrelevant to the question. If he was asking if sqlite supports dates and times, the answer would be yes. – Mooing Duck Dec 31 '15 at 01:09