sqlite

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 8, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

README

go-sqlite

A thin wrapper around Go's database/sql for SQLite. Automatically uses CGO (mattn/go-sqlite3) when available, pure Go (modernc.org/sqlite) otherwise.

Install

go get github.com/meysam/go-sqlite

Usage

db, err := sqlite.NewDB(context.Background(), "app.db")
if err != nil {
    log.Fatal(err)
}
defer db.Close()

db.Exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)")

Configuration

Functional Options
db, err := sqlite.NewDB(ctx, "app.db",
    sqlite.WithConnMaxOpen(10),
    sqlite.WithConnMaxLifetime(600),
    sqlite.WithConnMaxIdleTime(60),
    sqlite.WithMode("rwc"),
    sqlite.WithJournalMode("wal"),
)
Config Struct

Useful when loading from JSON/YAML config files:

cfg := sqlite.Config{
    ConnMaxOpen:     10,
    ConnMaxLifetime: 600,
    JournalMode:     "wal",
}
db, err := sqlite.NewDB(ctx, "app.db", sqlite.WithConfig(cfg))

Options

Option Default Description
ConnMaxOpen 1 Max open connections
ConnMaxLifetime 600 Connection max lifetime (seconds)
ConnMaxIdleTime 60 Connection max idle time (seconds)
Mode "rwc" SQLite mode (ro, rw, rwc, memory)
JournalMode "wal" Journal mode (delete, truncate, persist, memory, wal, off)

License

Apache 2.0

Documentation

Overview

Package sqlite provides a thin wrapper around database/sql for SQLite.

It automatically selects the appropriate driver: CGO-based (mattn/go-sqlite3) when CGO is enabled, or pure Go (modernc.org/sqlite) otherwise.

Basic usage:

db, err := sqlite.NewDB(ctx, "app.db")
if err != nil {
    log.Fatal(err)
}
defer db.Close()

With functional options:

db, err := sqlite.NewDB(ctx, "app.db",
    sqlite.WithConnMaxOpen(10),
    sqlite.WithJournalMode("wal"),
)

With config struct (useful for loading from config files):

cfg := sqlite.Config{ConnMaxOpen: 10, JournalMode: "wal"}
db, err := sqlite.NewDB(ctx, "app.db", sqlite.WithConfig(cfg))

Index

Constants

View Source
const (
	ENGINE = "sqlite3"
)

Variables

This section is empty.

Functions

func NewDB

func NewDB(ctx context.Context, filepath string, opts ...func(*connectionOption)) (*sql.DB, error)

NewDB creates a new SQLite database connection with the specified options. Provide the filepath only as the relative or absolute path to the database file. For example "test.db", "./my/relative/path/to/test.db", or "/absolute/path/to/test.db". For options, you can use the provided functions to set the connection parameters.

func WithConfig

func WithConfig(cfg Config) func(*connectionOption)

WithConfig applies a Config struct as connection options. Zero values in the config are ignored, preserving defaults.

func WithConnMaxIdleTime

func WithConnMaxIdleTime(connMaxIdleTime int) func(*connectionOption)

func WithConnMaxLifetime

func WithConnMaxLifetime(connMaxLifetime int) func(*connectionOption)

func WithConnMaxOpen

func WithConnMaxOpen(connMaxOpen int) func(*connectionOption)

func WithJournalMode

func WithJournalMode(journalMode string) func(*connectionOption)

func WithMode

func WithMode(mode string) func(*connectionOption)

Types

type Config

type Config struct {
	ConnMaxLifetime int
	ConnMaxIdleTime int
	ConnMaxOpen     int
	Mode            string
	JournalMode     string
}

Config holds SQLite connection configuration. Use with WithConfig when loading settings from config files. Zero values are ignored, allowing partial configuration.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL