sqlite

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: Apache-2.0 Imports: 7 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Default

func Default() *sql.DB

Default returns the default *sql.DB instance created by InitDefault.

func InitDefault

func InitDefault(conf *Config) error

InitDefault initializes the default singleton SQLite connection.

Example

ExampleInitDefault shows the singleton pattern: call InitDefault once at application startup. Subsequent calls are silently ignored (sync.Once). After a successful call, Default returns the initialised *sql.DB which can be passed directly to sqlc-generated Queries.

package main

import (
	"fmt"

	"github.com/phcp-tech/common-library-golang/dbsqlc/sqlite"
)

func main() {
	err := sqlite.InitDefault(&sqlite.Config{Path: ":memory:"})
	fmt.Println(err)
	fmt.Println(sqlite.Default() != nil)
}
Output:
<nil>
true

func NewSQLite

func NewSQLite(conf *Config) (*sql.DB, error)

NewSQLite opens a *sql.DB connection to SQLite. modernc.org/sqlite is a pure-Go driver (no CGO required).

For file-based databases, WAL mode and foreign key enforcement are recommended:

conf := &Config{Path: "file:app.db?_journal_mode=WAL&_foreign_keys=on"}
Example (File)

ExampleNewSQLite_file shows URI query parameters for enabling WAL mode and foreign key enforcement on a file-based SQLite database. WAL mode allows concurrent readers while a write is in progress.

package main

import (
	"fmt"

	"github.com/phcp-tech/common-library-golang/dbsqlc/sqlite"
)

func main() {
	db, err := sqlite.NewSQLite(&sqlite.Config{
		Path: "file::memory:?_journal_mode=WAL&_foreign_keys=on",
	})
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	defer db.Close()
	fmt.Println("opened")
}
Output:
opened
Example (Memory)

ExampleNewSQLite_memory shows how to open an ephemeral in-memory SQLite database. In-memory databases are lost when the *sql.DB is closed; they are ideal for tests and short-lived operations that require no persistence.

package main

import (
	"fmt"

	"github.com/phcp-tech/common-library-golang/dbsqlc/sqlite"
)

func main() {
	db, err := sqlite.NewSQLite(&sqlite.Config{Path: ":memory:"})
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	defer db.Close()
	fmt.Println("opened")
}
Output:
opened

Types

type Config

type Config struct {
	// Path is the path to the SQLite database file, or ":memory:" for an in-memory database.
	// URI format is supported: "file:app.db?_journal_mode=WAL&_foreign_keys=on"
	Path string
}

Config holds SQLite connection parameters.

Directories

Path Synopsis
Package component provides SQLite lifecycle integration for bootstrap.
Package component provides SQLite lifecycle integration for bootstrap.
Package vfs opens a SQLite database embedded in a Go binary via modernc.org/sqlite/vfs.
Package vfs opens a SQLite database embedded in a Go binary via modernc.org/sqlite/vfs.

Jump to

Keyboard shortcuts

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