tips
27 tips Sqlite
One command, setting or habit at a time about Sqlite: what it does, the line to copy and a caveat or two.
Admin and checks 9
-
Sets a SQLite file up so you can return free pages to disk without a full VACUUM.
sqlite3 app.db "PRAGMA auto_vacuum = INCREMENTAL; VACUUM;" -
Moves everything from the write-ahead log into the main file and resets the log to zero bytes.
sqlite3 app.db "PRAGMA wal_checkpoint(TRUNCATE);" -
Hashes the rows of a SQLite file so two copies can be compared regardless of file layout.
sqlite3 app.db ".sha3sum" -
Checks how many empty pages a SQLite file holds before you rebuild it.
sqlite3 -header app.db "SELECT * FROM pragma_page_count, pragma_freelist_count, pragma_page_size;" -
Turns on foreign key enforcement for a SQLite connection, since it is off by default.
sqlite> PRAGMA foreign_keys = ON; -
Sets how long a SQLite connection waits for a locked database before it reports an error.
sqlite> PRAGMA busy_timeout = 5000; -
Reads or sets a free integer in the SQLite file header, handy for tracking the schema version.
sqlite3 app.db "PRAGMA user_version = 3;" -
Switches a SQLite database to write-ahead logging, so readers and a writer stop blocking each other.
sqlite3 app.db "PRAGMA journal_mode=WAL;" -
Makes a SQLite table refuse values that don't match the column's declared type.
CREATE TABLE readings (
SQL shells 9
-
Makes a SQL script stop at the first failing statement instead of running the rest.
sqlite3 -bail app.db < migrate.sql -
Writes one query result to a file from inside the shell, then output goes back to the screen.
sqlite> .once users.csv -
Gets back ids and values of changed rows from the same statement that changed them.
DELETE FROM users WHERE active = 0 RETURNING id, email; -
Writes the CREATE and INSERT statements for just one table to move it to another file.
sqlite3 app.db ".dump users" > users.sql -
Pulls fields out of a JSON column with short arrow syntax instead of function calls.
SELECT prefs ->> 'theme', prefs -> 'theme', prefs ->> '$.lang' -
Inserts a row or updates the existing one in a single statement, without a SELECT first.
INSERT INTO counter VALUES ('home', 1) -
Binds values to :name placeholders so you can run an app's query exactly as written.
sqlite> .parameter set :mail '[email protected]' -
Opens a second SQLite file under a name so you can join or copy between both.
sqlite> ATTACH 'old.db' AS old; -
Shows sqlite3 query results as aligned columns with their names above them.
.mode column
Query tuning 7
-
Lists foreign key columns without an index and prints the CREATE INDEX to fix each one.
CREATE INDEX 'orders_user_id' ON 'orders'('user_id'); --> users(id) -
Indexes only rows that match a WHERE clause, so the index stays small and fast.
CREATE INDEX users_active_email ON users(email) WHERE active = 1; -
Declares a column case-insensitive so comparisons, uniqueness and its index ignore case.
email TEXT COLLATE NOCASE UNIQUE -
Searches text by words and prefixes with ranked results instead of slow LIKE scans.
CREATE VIRTUAL TABLE notes USING fts5(title, body); -
Updates SQLite's query planner statistics only for tables where that is likely to help.
sqlite3 app.db "PRAGMA optimize;" -
Proposes missing indexes for a query you type in the SQLite shell.
sqlite> .expert -
Prints the time each statement took in the sqlite3 shell, so you can compare queries.
.timer on
Queries and tables 2
-
Converts a date or time string into a Unix timestamp directly in SQL.
SELECT unixepoch('2026-01-01 00:00:00'); -
Picks a value based on conditions, without writing a full CASE expression.
SELECT name, iif(qty > 0, 'in stock', 'sold out') AS state
Nothing in this filter yet.