Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func InitDefault ¶
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 ¶
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