Sebastiaan de Schaetzen a377448de3
All checks were successful
Build / build (push) Successful in 1m22s
Add readme
2025-03-16 09:31:00 +01:00
2025-02-18 12:53:51 +01:00
2025-02-18 04:47:26 +01:00
2025-02-18 12:53:51 +01:00
2025-03-06 10:25:10 +01:00
2025-02-18 17:21:15 +01:00
2025-02-18 12:53:51 +01:00
2025-03-06 10:25:10 +01:00
2025-03-16 09:31:00 +01:00
2025-03-06 10:25:10 +01:00

MySQLite

A Go library that provides a convenient wrapper around SQLite with additional functionality for database management, migrations, and transactions.

Features

  • Simple and intuitive SQLite database connection management
  • Thread-safe database operations with built-in locking mechanism
  • Support for database migrations
  • Transaction management
  • Built on top of zombiezen.com/go/sqlite

Installation

go get gitea.seeseepuff.be/seeseemelk/mysqlite

Usage

Opening a Database Connection

import "gitea.seeseepuff.be/seeseemelk/mysqlite"

// Open an in-memory database
db, err := mysqlite.OpenDb(":memory:")
if err != nil {
    // Handle error
}
defer db.Close()

// Open a file-based database
db, err := mysqlite.OpenDb("path/to/database.db")
if err != nil {
    // Handle error
}
defer db.Close()

Executing Queries

The library provides methods for executing SQL queries and managing transactions:

// Execute a simple query
err := db.Query("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)").Exec()

// Use transactions
tx, err := db.BeginTransaction()
if err != nil {
    // Handle error
}

// Perform operations within transaction
// ...

// Commit or rollback
err = tx.Commit() // or tx.Rollback()

Database Migrations

The library includes support for SQL-based migrations. Migrations are SQL files stored in a directory and are executed in order based on their filename prefix:

  1. Create a directory for your migrations (e.g., migrations/)
  2. Add numbered SQL migration files:
    migrations/
    ├── 1_initial.sql
    ├── 2_add_users.sql
    ├── 3_add_posts.sql
    
  3. Embed the migrations in your Go code:
    import "embed"
    
    //go:embed migrations/*.sql
    var migrations embed.FS
    
    // Apply migrations
    err := db.MigrateDb(migrations, "migrations")
    if err != nil {
        // Handle error
    }
    

Each migration file should contain valid SQL statements. The migrations are executed in order and are tracked internally to ensure they only run once.

Requirements

  • Go 1.24 or higher
Description
No description provided
Readme 101 KiB
v0.12.0 Latest
2025-05-14 15:21:48 +02:00
Languages
Go 100%