SQLite for Go

github.com/malcolmston/sqlite

A pure-Go embedded SQL engine with a database/sql driver.

 GitHubports sqlite/sqlite

A small, dependency-free SQL database engine written in pure Go (standard library only, no cgo). It ships a SQL tokenizer, a recursive-descent parser and a tree-walking executor over an in-memory, row-oriented store, and plugs into the standard database/sql package through a registered driver named "mstsqlite". The engine implements a genuinely useful subset of SQL — CREATE TABLE / INSERT / SELECT (WHERE, GROUP BY, HAVING, ORDER BY, LIMIT, a two-table INNER JOIN and aggregates), UPDATE, DELETE and transactions — with SQLite-style dynamic typing and three-valued NULL logic. Import path github.com/malcolmston/sqlite; package sqlite.

Install

shell
$ go get github.com/malcolmston/sqlite

Quick start

main.go
import (
    "database/sql"
    "fmt"

    _ "github.com/malcolmston/sqlite" // registers the "mstsqlite" driver
)

db, _ := sql.Open("mstsqlite", ":memory:")
db.SetMaxOpenConns(1) // pin the anonymous in-memory DB to one connection

db.Exec(`CREATE TABLE fruit (name TEXT, qty INTEGER)`)
db.Exec(`INSERT INTO fruit VALUES (?, ?), (?, ?)`, "apple", 3, "banana", 7)

rows, _ := db.Query(`SELECT name, qty FROM fruit WHERE qty >= ? ORDER BY qty DESC`, 5)
for rows.Next() {
    var name string
    var qty int
    rows.Scan(&name, &qty)
    fmt.Printf("%s: %d\n", name, qty)
}

Features

  • Registers the "mstsqlite" database/sql driver (see DriverName) — open with sql.Open("mstsqlite", ":memory:")
  • Full DDL/DML subset — CREATE TABLE (typed columns, PRIMARY KEY/NOT NULL, IF NOT EXISTS), INSERT, UPDATE, DELETE, DROP TABLE
  • Rich SELECTWHERE, GROUP BY, HAVING, ORDER BY, LIMIT/OFFSET, DISTINCT, AS aliases and a two-table INNER JOIN
  • Aggregates — COUNT (incl. COUNT(*) / COUNT(DISTINCT x)), SUM, AVG, MIN, MAX
  • Expressions — comparisons, AND/OR/NOT, IN, LIKE (%/_), IS NULL, arithmetic and || concatenation
  • Transactions via BEGIN/COMMIT/ROLLBACK — snapshot rollback, serializable isolation (one writer at a time)
  • SQLite-style dynamic typing — the Value/ValueType storage classes (NULL, INTEGER, REAL, TEXT, BLOB) with three-valued NULL logic
  • Direct, non-database/sql API — NewDatabase, Database.Exec, Database.Query (returning ResultSet/ExecResult) and Parse for AST inspection
SQLite