English | 简体中文
A CGo-free SQLite driver for Go's database/sql package, based on the SQLite 3.53.0 amalgamation.
Originally developed by CZ.NIC z.s.p.o. and contributors at modernc.org/sqlite. This repository is an independent continuation with a restructured module layout.
The original project is a monolithic codebase containing over 50 tightly coupled Go modules, including C parsers, compilers, assemblers, and code generation toolchains. While the engineering is substantial, this architecture presents challenges for downstream users:
- Dependency surface — Importing the SQLite driver transitively pulls in lexers, IR frameworks, and compilation toolchains that are not required for database operations.
- Audit complexity — Hundreds of thousands of lines of generated and hand-written code span the full toolchain, making focused security review difficult.
- Maintenance coupling — Changes to low-level modules propagate unpredictably through the dependency graph, increasing the risk of unintended regressions.
- Contribution overhead — The monolithic structure and interdependent build process create a steep onboarding curve for contributors targeting the driver itself.
This repository addresses these concerns by restructuring the project as a focused, self-contained module with clear boundaries — easier to audit, maintain, and extend.
- CGo-free: Pure Go implementation, cross-compiles easily
- Full
database/sqldriver compatibility - Virtual Tables (vtab) in pure Go
- Custom SQL functions (scalar and aggregate)
- Custom collation sequences
- Online backup and restore
- Pre-update, commit, and rollback hooks
- Serialize/deserialize databases
- WAL mode support
- Context cancellation
- Multiple time format support
- 17+ OS/arch combinations
go get github.com/next-bin/go-sqlite/v2
import (
"database/sql"
_ "github.com/next-bin/go-sqlite/v2"
)
func main() {
db, err := sql.Open("sqlite", ":memory:")
if err != nil {
log.Fatal(err)
}
defer db.Close()
// ... use db with database/sql interface
}See the Go package documentation for full API details.
The examples/ directory contains runnable programs demonstrating all driver features:
file_basic— Basic database/sql operations with a file-backed databasebasics— CRUD, connection modes, data typestransactions— Transactions, savepointsfunctions— Custom scalar and aggregate functionscollation— Custom collation sequencesbackup— Online backup and restorehooks— Pre-update, commit, rollback hooksadvanced_types— Time formats, blob, NULL, boolconcurrency— Concurrent goroutine accessserialization— Serialize/deserialize databasescontext_cancel— Cancel queries via contextwal— WAL mode and checkpointcolumninfo— Column metadata queriesvtab_basic,vtab_csv,vtab_match,vtab_regexp— Virtual table examples
The driver exposes a Go API to implement SQLite virtual table modules in pure Go via the github.com/next-bin/go-sqlite/v2/vtab package. This lets you back SQL tables with arbitrary data sources (e.g., vector indexes, CSV files, remote APIs) and integrate with SQLite's planner.
- Register:
vtab.RegisterModule(db, name, module). Registration applies to new connections only. - Schema declaration: Call
ctx.Declare("CREATE TABLE <name>(<cols...>)")withinCreateorConnect. - Planning (BestIndex): Inspect
info.Constraints,info.OrderBy, andinfo.ColUsed. SetArgIndexandOmitto control constraint handling. - Execution:
Cursor.Filter(idxNum, idxStr, vals)receives arguments in the order implied byArgIndex. - Operators: Common SQLite operators map to
ConstraintOp(EQ/NE/GT/GE/LT/LE/MATCH/LIKE/GLOB/REGEXP etc.).
Based on modernc.org/sqlite by CZ.NIC z.s.p.o. Original SQLite by D. Richard Hipp (Public Domain).