Is there a dedicated boolean data type in SQLite?
SQLite and Boolean Values
- Instead, it employs integers to represent boolean data:
- 0 signifies
false
.
- 0 signifies
- SQLite, a popular lightweight database engine, doesn't have a distinct data type specifically for boolean values (true/false).
Boolean Literals
- Since SQLite interprets integers for boolean values, boolean literals in this context would be integer literals (0 or 1).
- You could write
0
to representfalse
or1
to representtrue
.
- You could write
- In programming, literals are fixed values directly included in the code.
Alternative for Modern SQLite (Version 3.23.0 and Later)
- As of version 3.23.0 (released in April 2018), SQLite introduced keywords for boolean literals:
TRUE
representstrue
.FALSE
representsfalse
.
Key Points
- Integer literals (0 and 1) still work perfectly for representing boolean data.
- While technically not separate boolean literals,
TRUE
andFALSE
keywords offer a more readable way to express boolean values in your SQLite queries (especially if you're coming from other SQL databases that have dedicated boolean data types).
Example
-- Using integer literals (traditional way)
SELECT * FROM table_name WHERE is_active = 1; -- Selects rows where is_active is true (1)
-- Using keywords (modern SQLite)
SELECT * FROM table_name WHERE is_active = TRUE; -- Same result, but more explicit
Traditional Method (Integer Literals):
-- Create a table with a boolean column (represented by integer)
CREATE TABLE users (
id INTEGER PRIMARY KEY,
is_admin INTEGER DEFAULT 0 -- Default value is false (0)
);
-- Insert data with both true and false values
INSERT INTO users (is_admin) VALUES (1), (0);
-- Select users based on boolean condition
SELECT * FROM users WHERE is_admin = 1; -- Selects users with admin privileges (is_admin is true/1)
SELECT * FROM users WHERE is_admin = 0; -- Selects non-admin users (is_admin is false/0)
Modern Method (Boolean Keywords - SQLite 3.23.0 and Later):
-- Create a table (same structure)
CREATE TABLE users (
id INTEGER PRIMARY KEY,
is_admin INTEGER DEFAULT 0
);
-- Insert data (same)
INSERT INTO users (is_admin) VALUES (1), (0);
-- Select users using boolean keywords
SELECT * FROM users WHERE is_admin = TRUE; -- Selects users with admin privileges
SELECT * FROM users WHERE is_admin = FALSE; -- Selects non-admin users
-
Custom Functions (for Specific Needs):
If you have a specific use case where integer literals (0/1) or even the keywords (TRUE/FALSE) don't quite fit your needs, you can create a custom function in SQLite. This function could take an argument and return 0 or 1 based on your logic.
Here's a basic example:
CREATE FUNCTION is_enabled(value TEXT) RETURNS INTEGER AS $$ BEGIN IF value = 'on' THEN RETURN 1; -- Treat 'on' as true ELSE RETURN 0; -- Treat anything else as false END IF; END; $$ LANGUAGE plpgsql; -- Adjust language based on your SQLite environment SELECT * FROM table_name WHERE is_enabled(status_column) = 1;
This example defines a function
is_enabled
that checks if the providedvalue
is "on" (treated as true) and returns 1. Otherwise, it returns 0 (false). -
Enum Data Type (SQLite 3.35.0 and Later):
If you're using a more recent version of SQLite (3.35.0 and above), you can leverage the
ENUM
data type. This allows you to define a set of allowed values for a column, which could include options like "true" and "false" for boolean representation.CREATE TABLE settings ( id INTEGER PRIMARY KEY, is_active ENUM('true', 'false') DEFAULT 'false' ); -- Insert data with 'true' or 'false' values INSERT INTO settings (is_active) VALUES ('true'), ('false'); SELECT * FROM settings WHERE is_active = 'true';
Note: Using custom functions or enums might introduce some overhead compared to simple integer literals or keywords. Choose the approach that best suits your specific requirements and database version.
sqlite boolean literals