This package provides a low-level Go interface to SQLite 3 designed to take maximum advantage of multi-threading. Connections are pooled and take advantage of the SQLite shared cache mode and the package takes advantage of the unlock-notify API to minimize the amount of handling user code needs for dealing with database lock contention.
It has interfaces for some of SQLite's more interesting extensions, such as incremental BLOB I/O and the session extension.
A utility package, sqliteutil, provides some higher-level tools for making it easier to perform common tasks with SQLite. In particular it provides support to make nested transactions easy to use via sqliteutil.Save.
This is not a database/sql driver.
go get -u crawshaw.io/sqlite
A HTTP handler that uses a multi-threaded pool of SQLite connections via a shared cache.
var dbpool *sqlite.Pool
func main() {
var err error
dbpool, err = sqlite.Open("file:memory:?mode=memory", 0, 10)
if err != nil {
log.Fatal(err)
}
http.Handle("/", handler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
func handle(w http.ResponseWriter, r *http.Request) {
conn := dbpool.Get(r.Context().Done())
if conn == nil {
return
}
defer dbpool.Put(conn)
stmt := conn.Prep("SELECT foo FROM footable WHERE id = $id;")
stmt.SetText("$id", "_user_id_")
for {
if hasRow, err := stmt.Step(); err != nil {
// ... handle error
} else if !hasRow {
break
}
foo := stmt.GetText("foo")
// ... use foo
}
}