Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a boolean literal in SQLite?

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

like image 880
Benjamin Oakes Avatar asked Mar 24 '10 19:03

Benjamin Oakes


People also ask

Does SQLite have Boolean?

SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored as integers 0 (false) and 1 (true). SQLite recognizes the keywords "TRUE" and "FALSE", as of version 3.23.

How do I create a boolean in SQLite?

Basically, SQLite does not support the Boolean data type, so instead of Boolean type SQLite uses the integer data type to execute the Boolean expression in the form of true or false here 0 is used for false and 1 for true that we already discussed.

How does SQLite compare to Boolean?

SQLite has no built-in boolean type - you have to use an integer instead. Also, when you're comparing the value to 'TRUE' and 't', you're comparing it to those values as strings, not as booleans or integers, and therefore the comparison will always fail.

What is real type in SQLite?

Real values are real numbers with decimal values that use 8-byte floats. TEXT. TEXT is used to store character data. The maximum length of TEXT is unlimited. SQLite supports various character encodings.


2 Answers

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.

like image 101
Justin Ethier Avatar answered Oct 05 '22 21:10

Justin Ethier


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

like image 40
Lukasz Szozda Avatar answered Oct 05 '22 20:10

Lukasz Szozda