Documentation
¶
Overview ¶
Package sqlite3 provides a thin cgo wrapper around the SQLite3 C library exposing a small, convenient API for opening databases, executing SQL, preparing statements and performing bulk inserts.
Notes and important details:
- This package uses cgo to call into the SQLite C API. Care is taken to manage C memory (C.CString / C.free) but callers should still be mindful of long-lived C pointers and thread-safety when using C libraries.
- BulkInsert uses a small C helper to batch many inserts in a single transaction to reduce cgo overhead.
Index ¶
- type Config
- type OpenFlag
- type SQLite3
- func (db *SQLite3) Begin() error
- func (s *SQLite3) BulkInsert(query string, values [][]string) error
- func (s *SQLite3) Changes() int
- func (s *SQLite3) Close() error
- func (db *SQLite3) Commit() error
- func (s *SQLite3) Config(config Config, value int) error
- func (db *SQLite3) EnableAutoVacuumMode(enable bool) error
- func (db *SQLite3) EnableDefaultJournalMode() error
- func (db *SQLite3) EnableForeignKeyConstraints(enable bool) error
- func (db *SQLite3) EnableSynchronousMode(enable bool) error
- func (db *SQLite3) EnableWalMode(enable bool) error
- func (s *SQLite3) ErrorCode() int
- func (s *SQLite3) ErrorMsg() string
- func (s *SQLite3) Exec(query string) error
- func (s *SQLite3) LastInsertRowID() int64
- func (s *SQLite3) Prepare(query string) (*SQLite3Stmt, error)
- func (s *SQLite3) Query(query string) (*SQLite3Stmt, error)
- func (db *SQLite3) Rollback() error
- func (s *SQLite3) TotalChanges() int
- func (db *SQLite3) Transaction(fn func() error) error
- type SQLite3Stmt
- func (s *SQLite3Stmt) Bind(args ...any) error
- func (s *SQLite3Stmt) Close() error
- func (s *SQLite3Stmt) ColumnBlob(index int) []byte
- func (s *SQLite3Stmt) ColumnBool(index int) bool
- func (s *SQLite3Stmt) ColumnFloat(index int) float64
- func (s *SQLite3Stmt) ColumnInt(index int) int
- func (s *SQLite3Stmt) ColumnInt64(index int) int64
- func (s *SQLite3Stmt) ColumnText(index int) string
- func (s *SQLite3Stmt) Err() error
- func (s *SQLite3Stmt) Exec(args ...any) error
- func (s *SQLite3Stmt) IsColumnNull(index int) bool
- func (s *SQLite3Stmt) Next() bool
- func (s *SQLite3Stmt) Reset() error
- func (s *SQLite3Stmt) Scan(dest ...any) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config int
Enum constants for sqlite3_db_config options
https://www.sqlite.org/c3ref/c_dbconfig_defensive.html
const ( SQLITE_DBCONFIG_MAINDBNAME Config = 1000 + iota // Configures the name of the main database. SQLITE_DBCONFIG_LOOKASIDE // Configures the lookaside memory allocator. SQLITE_DBCONFIG_ENABLE_FKEY // Enables or disables foreign key constraints. SQLITE_DBCONFIG_ENABLE_TRIGGER // Enables or disables triggers. SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER // Enables or disables FTS3 tokenizers. SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION // Enables or disables loading extensions. SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE // Configures the automatic checkpoint behavior. SQLITE_DBCONFIG_ENABLE_QPSG // Enables or disables the query planner stability guarantee. SQLITE_DBCONFIG_TRIGGER_EQP // Enables or disables the trigger evaluation plan. SQLITE_DBCONFIG_RESET_DATABASE // Configures the database reset behavior. SQLITE_DBCONFIG_DEFENSIVE // Configures the defensive mode setting. SQLITE_DBCONFIG_WRITABLE_SCHEMA // Configures the writable schema setting. SQLITE_DBCONFIG_LEGACY_ALTER_TABLE // Enables or disables legacy behavior for ALTER TABLE. SQLITE_DBCONFIG_DQS_DML // Enables or disables double-quoted string literals in DML statements. SQLITE_DBCONFIG_DQS_DDL // Enables or disables double-quoted string literals in DDL statements. SQLITE_DBCONFIG_ENABLE_VIEW // Enables or disables the use of views in SQL statements. SQLITE_DBCONFIG_LEGACY_FILE_FORMAT // Enables or disables the legacy file format. SQLITE_DBCONFIG_TRUSTED_SCHEMA // Enables or disables the trusted schema setting. SQLITE_DBCONFIG_STMT_SCANSTATUS // Enables or disables the statement scan status setting. SQLITE_DBCONFIG_REVERSE_SCANORDER // Enables or disables the reverse scan order setting. SQLITE_DBCONFIG_MAX = 1019 // The maximum value for a SQLiteConfig constant. )
type OpenFlag ¶
type OpenFlag int
OpenFlag represents flags for opening a SQLite database. See https://www.sqlite.org/c3ref/c_open_autoproxy.html
const ( SQLITE_OPEN_READONLY OpenFlag = 0x00000001 // Database opened read-only SQLITE_OPEN_READWRITE OpenFlag = 0x00000002 // Database opened for reading and writing SQLITE_OPEN_CREATE OpenFlag = 0x00000004 // Database will be created if it does not exist SQLITE_OPEN_URI OpenFlag = 0x00000040 // Filename is interpreted as a URI SQLITE_OPEN_MEMORY OpenFlag = 0x00000080 // Database will be opened as an in-memory database SQLITE_OPEN_NOMUTEX OpenFlag = 0x00008000 // Database connection opens in multi-thread mode SQLITE_OPEN_FULLMUTEX OpenFlag = 0x00010000 // Database connection opens in serialized mode SQLITE_OPEN_SHAREDCACHE OpenFlag = 0x00020000 // Database is opened with shared cache enabled SQLITE_OPEN_PRIVATECACHE OpenFlag = 0x00040000 // Database is opened with shared cache disabled SQLITE_OPEN_EXRESCODE OpenFlag = 0x02000000 // Extended result codes are enabled SQLITE_OPEN_NOFOLLOW OpenFlag = 0x01000000 // Do not follow symbolic links )
type SQLite3 ¶
type SQLite3 struct {
// contains filtered or unexported fields
}
func Open ¶
Opens a database connection with specified filename. The file is created if it does not exist. To customize open behavior, use OpenV2 with specific flags. See https://www.sqlite.org/c3ref/open.html
func OpenMemory ¶
OpenMemory opens an in-memory SQLite database. This is a convenience wrapper around OpenV2.
func OpenReadOnly ¶
OpenReadOnly opens a SQLite database in read-only mode. This is a convenience wrapper around OpenV2.
func OpenReadWrite ¶
OpenReadWrite opens a SQLite database in read-write mode, creating it if it doesn't exist. This is a convenience wrapper around OpenV2.
func OpenV2 ¶
OpenV2 opens a SQLite database with specific flags and an optional VFS module. The flags parameter controls how the database is opened (read-only, read-write, create, etc.). The vfs parameter specifies the name of the VFS module to use, or empty string for default. Returns a new SQLite3 connection on success, or an error if the operation fails.
func (*SQLite3) Begin ¶
Begin starts a transaction by executing `BEGIN TRANSACTION`. It returns any error produced by the underlying Exec call.
func (*SQLite3) BulkInsert ¶
BulkInsert performs a bulk insert operation on the SQLite3 database. All parameters are passed as a 2D slice of strings. The first dimension represents the rows, and the second dimension represents the columns. The number of columns must match the number of placeholders in the query. This is a convenience method for inserting multiple rows at once avoiding the CGO overhead. The query must contain placeholders for the values, e.g. "INSERT INTO table (col1, col2) VALUES (?, ?)".
func (*SQLite3) EnableAutoVacuumMode ¶
func (*SQLite3) EnableDefaultJournalMode ¶
default rollback journal mode
func (*SQLite3) EnableForeignKeyConstraints ¶
Enable foreign key constraints
func (*SQLite3) EnableSynchronousMode ¶
func (*SQLite3) EnableWalMode ¶
func (*SQLite3) LastInsertRowID ¶
func (*SQLite3) Prepare ¶
func (s *SQLite3) Prepare(query string) (*SQLite3Stmt, error)
Prepare creates a new statement.
func (*SQLite3) TotalChanges ¶
func (*SQLite3) Transaction ¶
Transaction runs `fn` inside a transaction. If `fn` returns an error the transaction is rolled back; otherwise it is committed. Any error from Begin/Commit/Rollback is returned to the caller.
type SQLite3Stmt ¶
type SQLite3Stmt struct {
// contains filtered or unexported fields
}
SQLite3Stmt is a lightweight wrapper around an underlying `sqlite3_stmt*`.
func (*SQLite3Stmt) Bind ¶
func (s *SQLite3Stmt) Bind(args ...any) error
Bind binds arguments to the statement parameters (1-based).
func (*SQLite3Stmt) ColumnBlob ¶
func (s *SQLite3Stmt) ColumnBlob(index int) []byte
ColumnBlob returns the value of the column as a byte slice.
func (*SQLite3Stmt) ColumnBool ¶
func (s *SQLite3Stmt) ColumnBool(index int) bool
ColumnBool returns the value of the column as a bool.
func (*SQLite3Stmt) ColumnFloat ¶
func (s *SQLite3Stmt) ColumnFloat(index int) float64
ColumnFloat returns the value of the column as a float64.
func (*SQLite3Stmt) ColumnInt ¶
func (s *SQLite3Stmt) ColumnInt(index int) int
ColumnInt returns the value of the column as an int.
func (*SQLite3Stmt) ColumnInt64 ¶
func (s *SQLite3Stmt) ColumnInt64(index int) int64
ColumnInt64 returns the value of the column as an int64.
func (*SQLite3Stmt) ColumnText ¶
func (s *SQLite3Stmt) ColumnText(index int) string
ColumnText returns the value of the column as a string.
func (*SQLite3Stmt) Err ¶
func (s *SQLite3Stmt) Err() error
Err returns the error that caused Next() to return false, if any.
func (*SQLite3Stmt) Exec ¶
func (s *SQLite3Stmt) Exec(args ...any) error
Exec binds arguments, executes one step, and resets. Returns error immediately if execution fails.
func (*SQLite3Stmt) IsColumnNull ¶
func (s *SQLite3Stmt) IsColumnNull(index int) bool
IsColumnNull checks if the column value is NULL.
func (*SQLite3Stmt) Next ¶
func (s *SQLite3Stmt) Next() bool
Next advances to the next row. It returns true if a row is available. It returns false if the query is done OR if an error occurred. Check Err() after the loop to distinguish.
func (*SQLite3Stmt) Reset ¶
func (s *SQLite3Stmt) Reset() error
Reset clears bindings and resets the statement execution state. It also clears any sticky error from previous iterations.
func (*SQLite3Stmt) Scan ¶
func (s *SQLite3Stmt) Scan(dest ...any) error
Scan reads values from the current row into dest.