Is there a boolean literal in SQLite?

前端 未结 12 1841
逝去的感伤
逝去的感伤 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:29

    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

提交回复
热议问题