Is there a boolean literal in SQLite?

前端 未结 12 1821
逝去的感伤
逝去的感伤 2020-12-04 20:43

I know about the boolean column type, but is there a boolean literal in SQLite? In other languages, this might be true

相关标签:
12条回答
  • 2020-12-04 21:32

    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.
    0 讨论(0)
  • 2020-12-04 21:35

    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
    
    0 讨论(0)
  • 2020-12-04 21:35

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

    0 讨论(0)
  • 2020-12-04 21:36

    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

    0 讨论(0)
  • 2020-12-04 21:38

    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 讨论(0)
  • 2020-12-04 21:44

    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

    0 讨论(0)
提交回复
热议问题