Skip to content

SQLite Driver Support

Choose a tag to compare

@bw19 bw19 released this 21 Mar 17:12
· 40 commits to main since this release

SQLite Driver Support

Sequel now supports SQLite via the pure-Go modernc.org/sqlite driver. No CGo or external libraries required.

Opening a connection

db, err := sequel.Open("sqlite", "file:path/to/database.db")
db, err := sequel.Open("sqlite", ":memory:")
db, err := sequel.Open("", "file:mydata.db") // driver inferred automatically

The driver name "sqlite3" is accepted as an alias for "sqlite".

Testing

SQLite is now the default driver for OpenTesting when no driver or DSN is provided. Tests use in-memory databases for speed and zero cleanup:

db, err := sequel.OpenTesting("", "", t.Name()) // SQLite in-memory

Virtual function support

All built-in virtual functions expand to SQLite-compatible SQL:

Function SQLite expansion
NOW_UTC() STRFTIME('%Y-%m-%d %H:%M:%f', 'now')
DATE_ADD_MILLIS(base, ms) STRFTIME('%Y-%m-%d %H:%M:%f', base, '+' || (ms/1000.0) || ' seconds')
DATE_DIFF_MILLIS(a, b) (JULIANDAY(a) - JULIANDAY(b)) * 86400000.0
LIMIT_OFFSET(l, o) LIMIT l OFFSET o
REGEXP_TEXT_SEARCH(? IN cols) cols LIKE ('%' || ? || '%')

Note: REGEXP_TEXT_SEARCH uses LIKE for substring matching since SQLite does not have built-in regex support. This is case-insensitive for ASCII but not a true regular expression match.

Other details

  • Placeholders: ? (same as MySQL and SQL Server — no conversion needed)
  • InsertReturnID: Uses LastInsertId() (same mechanism as MySQL)
  • Schema migration: Fully supported with SQLite-native datetime functions and INSERT OR IGNORE for concurrency safety
  • Driver-specific migration files: Use -- DRIVER: sqlite comments

Internal improvements

  • Removed hardcoded $1, $2 PostgreSQL placeholders from migration queries — these now use ? and rely on automatic placeholder conversion like all other query paths
  • Replaced driver-specific datetime expressions in migration logic with NOW_UTC() and DATE_ADD_MILLIS() virtual functions, collapsing many four-way switch statements into shared code
  • Default OpenTesting driver changed from MySQL to SQLite for zero-configuration testing