sqlite3

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2026 License: ISC Imports: 10 Imported by: 0

README

A concise Go database/sql/driver for SQLite v3 using cmd/cgo.

The following files define the implementation:

This driver strictly implements database/sql/driver by:

  • Following the exact structure of the documentation
  • Implementing all optional and deprecated interfaces for compatibility

Using

Below is a trivial example. See https://go.dev/wiki/SQLInterface for more examples.

package main

import (
  "context"
	"database/sql"
	"log"

	_ "codeberg.org/maudlin/sqlite3.go"
)

func main() {
	// Create a database handle
	db, err := sql.Open("sqlite3", ":memory:")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	// Create a context
	ctx := context.Background()

	// Verify a db connection can be made
	if err := db.PingContext(ctx); err != nil {
		log.Fatal(err)
	}

	// ...
}

Testing

go test -v

Documentation

Overview

sqlite driver for Go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conn

type Conn struct {
	// contains filtered or unexported fields
}

Conn represents a SQLite database connection.

func (*Conn) Begin deprecated

func (c *Conn) Begin() (driver.Tx, error)

Begin starts a new transaction.

Deprecated: Drivers should implement ConnBeginTx instead (or additionally).

func (*Conn) BeginTx

func (c *Conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error)

BeginTx starts a new transaction with the specified options.

func (*Conn) CheckNamedValue

func (c *Conn) CheckNamedValue(val *driver.NamedValue) error

CheckNamedValue validates a named value before binding.

This method handles driver.Valuer types by calling Value() to get the underlying value. It also handles time.Time conversion to ISO8601 format.

func (*Conn) Close

func (c *Conn) Close() error

Close closes the database connection.

func (*Conn) Exec deprecated

func (c *Conn) Exec(query string, args []driver.Value) (driver.Result, error)

Exec executes a query that doesn't return rows.

Deprecated: Use ExecContext instead.

func (*Conn) ExecContext

func (c *Conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error)

ExecContext executes a query that doesn't return rows, with context support.

func (*Conn) IsValid

func (c *Conn) IsValid() bool

IsValid returns whether the connection is valid.

func (*Conn) Ping

func (c *Conn) Ping(ctx context.Context) error

Ping verifies that the connection is still alive.

func (*Conn) Prepare

func (c *Conn) Prepare(query string) (driver.Stmt, error)

Prepare creates a prepared statement for later execution.

func (*Conn) PrepareContext

func (c *Conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error)

PrepareContext creates a prepared statement with context support.

func (*Conn) Query deprecated

func (c *Conn) Query(query string, args []driver.Value) (driver.Rows, error)

Query executes a query that returns rows.

Deprecated: Use QueryContext instead.

func (*Conn) QueryContext

func (c *Conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error)

QueryContext executes a query that returns rows, with context support.

func (*Conn) ResetSession

func (c *Conn) ResetSession(ctx context.Context) error

ResetSession resets the connection state. Implements the driver.SessionResetter interface.

func (*Conn) Validate

func (c *Conn) Validate() error

Validate verifies that the connection is still valid and usable. Implements the driver.Validator interface.

This method is called by the database/sql package before returning a connection from the connection pool to ensure it's still usable. It checks that the connection is not closed and that the SQLite database handle is still valid.

type Connector

type Connector struct {
	// contains filtered or unexported fields
}

Connector creates new connections for connection pooling. Connector enables connection pooling and allows reusing the same connection configuration across multiple database operations.

func (*Connector) Close

func (c *Connector) Close() error

Close closes the connector.

func (*Connector) Connect

func (c *Connector) Connect(ctx context.Context) (driver.Conn, error)

Connect creates a new connection.

func (*Connector) Driver

func (c *Connector) Driver() driver.Driver

Driver returns the Driver associated with the connector.

type Driver

type Driver struct{}

Driver is the SQLite driver implementation.

func (*Driver) DriverName

func (d *Driver) DriverName() string

DriverName returns the driver name.

func (*Driver) Open

func (d *Driver) Open(name string) (driver.Conn, error)

Open creates a new connection to the SQLite database.

Connection string options (comma-separated):

  • _foreign_keys=1: Enable foreign key constraints
  • _wal=1: Enable WAL mode (default: 1)
  • _busy_timeout=N: Set busy timeout in milliseconds (default: 5000)

Examples:

  • ":memory:" - In-memory database
  • "file.db" - File-based database
  • "file.db?_foreign_keys=1&_wal=1" - With options

func (*Driver) OpenConnector

func (d *Driver) OpenConnector(name string) (driver.Connector, error)

OpenConnector returns a Connector for the driver.

type Result

type Result struct {
	// contains filtered or unexported fields
}

Result is the result of a query execution that doesn't return rows.

func (*Result) LastInsertId

func (r *Result) LastInsertId() (int64, error)

LastInsertId returns the last inserted row ID.

func (*Result) RowsAffected

func (r *Result) RowsAffected() (int64, error)

RowsAffected returns the number of rows affected by the query.

type Rows

type Rows struct {
	// contains filtered or unexported fields
}

Rows is an iterator over the rows returned by a query.

func (*Rows) Close

func (r *Rows) Close() error

Close closes the Rows iterator and releases associated resources.

This method implements the driver.Rows interface and is automatically called by the database/sql package when rows are exhausted or explicitly closed. It ensures proper cleanup of SQLite statement handles to prevent resource leaks.

func (*Rows) ColumnTypeDatabaseTypeName

func (r *Rows) ColumnTypeDatabaseTypeName(index int) string

ColumnTypeDatabaseTypeName returns the database system type name for the column.

func (*Rows) ColumnTypeLength

func (r *Rows) ColumnTypeLength(index int) (length int64, ok bool)

ColumnTypeLength returns the length of the column type.

SQLite does not have fixed-length character types in the same way as other databases. Column declarations like VARCHAR(255) are stored as text, and SQLite does not enforce length constraints. This method returns (0, false) to indicate that the length is unknown.

This is a no-op implementation required by the driver.RowsColumnTypeLength interface.

func (*Rows) ColumnTypeNullable

func (r *Rows) ColumnTypeNullable(index int) (nullable, ok bool)

ColumnTypeNullable reports whether the column type is nullable.

func (*Rows) ColumnTypePrecisionScale

func (r *Rows) ColumnTypePrecisionScale(index int) (precision, scale int64, ok bool)

ColumnTypePrecisionScale returns the precision and scale for decimal types.

SQLite does not have native decimal types with precision/scale constraints. Numeric values are stored as flexible types (REAL or INTEGER), and precision/scale are not enforced at the database level. This method returns (0, 0, false) to indicate that precision/scale are unknown.

This is a no-op implementation required by the driver.RowsColumnTypePrecisionScale interface.

func (*Rows) ColumnTypeScanType

func (r *Rows) ColumnTypeScanType(index int) reflect.Type

ColumnTypeScanType returns the Go type that should be used when scanning.

func (*Rows) Columns

func (r *Rows) Columns() []string

Columns returns the names of the columns in the result set.

func (*Rows) HasNextResultSet

func (r *Rows) HasNextResultSet() bool

HasNextResultSet returns true if there are additional result sets.

SQLite does not support multiple result sets per query, so this method always returns false. This is a no-op implementation required by the driver.RowsNextResultSet interface to ensure compatibility with the database/sql package.

Note: Multiple result sets are not supported by SQLite. Each query returns a single result set, and this method will always return false even after calling NextResultSet.

func (*Rows) Next

func (r *Rows) Next(dest []driver.Value) error

Next advances the row cursor to the next row.

func (*Rows) NextResultSet

func (r *Rows) NextResultSet() error

NextResultSet advances to the next result set.

SQLite does not support multiple result sets per query, so this method always returns nil. This is a no-op implementation required by the driver.Rows interface to ensure compatibility with the database/sql package.

Note: Multiple result sets are not supported by SQLite. Each query returns a single result set, and this method will always return nil.

type Stmt

type Stmt struct {
	// contains filtered or unexported fields
}

Stmt represents a prepared statement.

func (*Stmt) CheckNamedValue

func (s *Stmt) CheckNamedValue(val *driver.NamedValue) error

CheckNamedValue validates a named value before binding.

This method handles driver.Valuer types by calling Value() to get the underlying value. It also handles time.Time conversion to ISO8601 format.

This implementation mirrors Conn.CheckNamedValue to ensure consistent type handling whether using prepared statements or direct queries.

func (*Stmt) Close

func (s *Stmt) Close() error

Close closes the prepared statement and releases associated resources.

This method implements the driver.Stmt interface and is automatically called by the database/sql package when the statement is no longer needed. It ensures proper cleanup of SQLite statement handles to prevent resource leaks.

func (*Stmt) ColumnConverter deprecated

func (s *Stmt) ColumnConverter(idx int) driver.ValueConverter

ColumnConverter returns a ValueConverter for the column.

Deprecated: Use CheckNamedValue or implement custom conversion logic.

func (*Stmt) Exec deprecated

func (s *Stmt) Exec(args []driver.Value) (driver.Result, error)

Exec executes the statement with the given arguments.

Deprecated: Drivers should implement StmtExecContext instead (or additionally).

func (*Stmt) ExecContext

func (s *Stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error)

ExecContext executes the statement with the given arguments, with context support.

func (*Stmt) NumInput

func (s *Stmt) NumInput() int

NumInput returns the number of placeholder parameters.

func (*Stmt) Query deprecated

func (s *Stmt) Query(args []driver.Value) (driver.Rows, error)

Query executes the statement and returns an iterator over the rows.

Deprecated: Drivers should implement StmtQueryContext instead (or additionally).

func (*Stmt) QueryContext

func (s *Stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error)

QueryContext executes the statement and returns an iterator over the rows, with context support.

type Tx

type Tx struct {
	// contains filtered or unexported fields
}

Tx represents a database transaction.

func (*Tx) Commit

func (tx *Tx) Commit() error

Commit commits the transaction.

func (*Tx) Rollback

func (tx *Tx) Rollback() error

Rollback aborts the transaction.

Jump to

Keyboard shortcuts

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