Skip to content

SQLite SQL API

SQLite SQL dialect for creating tables, inserting rows, querying data and inspecting schema metadata via PRAGMA.

1 class · 4 methods

SQL Statements

4 methods

Core SQLite DDL, DML and PRAGMA statements.

CREATE TABLE [IF NOT EXISTS] tbl (col type [constraint], ..., [table_constraint])

Creates a new table. SQLite uses dynamic typing; common affinities are TEXT, INTEGER, REAL, BLOB, NUMERIC.

Parameters

NameTypeDescription
tblidentifierTable name.
typeaffinityType affinity such as INTEGER or TEXT.
constraintconstraintPRIMARY KEY, NOT NULL, DEFAULT, UNIQUE, CHECK, REFERENCES.

Returns

void

Example

sqlite
CREATE TABLE IF NOT EXISTS notes (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  body TEXT NOT NULL,
  created_at TEXT DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO tbl [(cols)] VALUES (vals) | SELECT ... [ON CONFLICT(cols) DO ...]

Inserts rows into a table either by literal values or by selecting from another query.

Parameters

NameTypeDescription
tblidentifierTarget table.
colscolumn-listOptional column list.
valsvalue-listLiteral values matching cols.

Returns

void

Example

sqlite
INSERT INTO notes (body) VALUES ('hello')
ON CONFLICT(id) DO UPDATE SET body = excluded.body;
SELECT cols FROM tbl [WHERE condition] [ORDER BY cols] [LIMIT n]

Reads rows from a table with optional filtering, ordering and limiting.

Parameters

NameTypeDescription
tblidentifierSource table or subquery.
conditionbooleanFilter predicate.

Returns

ResultSet

Example

sqlite
SELECT id, body FROM notes WHERE body LIKE '%hello%' ORDER BY created_at DESC LIMIT 10;
PRAGMA name [= value]

Queries or sets SQLite compile-time and runtime options such as foreign_keys, journal_mode, table_info.

Parameters

NameTypeDescription
namestringPragma name, e.g. foreign_keys, journal_mode, table_info(tbl).
valueanyOptional new value for the pragma.

Returns

ResultSet | void

Example

sqlite
PRAGMA foreign_keys = ON;
PRAGMA table_info(notes);