SQLITE GO TUTORIAL · DATABASE/SQL · 7 LESSONS
SQLite from Go: connections, drivers and types
database/sql and lazy opens, drivers and DSN pragmas, :memory: and the pool, type affinity and STRICT tables, rowid keys, NULLs, time values and foreign keys.
Open a SQLite database from Go: database/sql, a driver and a file
Open a file database through database/sql, see that sql.Open connects lazily, and run Exec, Query and QueryRow with placeholders and complete error handling.
5 min · Worked lesson
Choose a SQLite driver and configure every connection through the DSN
Compare modernc.org/sqlite, mattn/go-sqlite3 and ncruces/go-sqlite3, and use DSN parameters so that every pooled connection gets the same pragmas and transaction mode.
4 min · Worked lesson
:memory: is per connection: the pool, SetMaxOpenConns(1), memdb and shared cache
Explain why an in-memory database vanishes between pooled connections and choose between a single connection, the memdb VFS, shared cache and a temporary file.
3 min · Worked lesson
Type affinity, dynamic typing and STRICT tables
Predict the storage class SQLite gives each value from a column's declared type, see what Go receives when scanning into any, and use STRICT tables to reject values that do not fit.
3 min · Worked lesson
INTEGER PRIMARY KEY, rowid, AUTOINCREMENT and WITHOUT ROWID
Recognize when a primary key is an alias for the rowid, decide whether AUTOINCREMENT is worth its cost, and know when a WITHOUT ROWID table fits.
4 min · Worked lesson
NULLs and time values: sql.Null, pointers, COALESCE and a time format you choose
Scan nullable columns with sql.NullString, sql.Null[T] and pointers, write NULL-aware SQL, and store times in a format that sorts and that SQLite's date functions understand.
3 min · Worked lesson
Foreign keys are off until each connection turns them on
Show that REFERENCES clauses are not enforced by default, enable enforcement for every connection through the DSN, use ON DELETE CASCADE and find existing violations with foreign_key_check.
3 min · Worked lesson