Sqinn-Go allows you to execute SQL commands and query rows without using cgo. It works by launching a sqinn child process and communicating via stdin/stdout.
Key workflow:
- Launch an instance using
sqinn.MustLaunch with sqinn.Options. - Execute commands using
MustExecSql or MustExecParams. - Query data using
MustQueryRows, specifying the types of columns you wish to fetch. - Close the instance with
Close().
Note: Sqinn-Go is not a database/sql driver. It uses higher-level interfaces to minimize the inter-process communication (IPC) overhead.
import (
"fmt"
"github.com/cvilsmeier/sqinn-go/v2"
)
func main() {
// Launch sqinn, close when done.
sq := sqinn.MustLaunch(sqinn.Options{
Db: ":memory:", // use a transient in-memory database
})
defer sq.Close()
// Create a table, cleanup when done
sq.MustExecSql("CREATE TABLE users (id INTEGER PRIMARY KEY NOT NULL, name TEXT)")
defer sq.MustExecSql("DROP TABLE users")
// Insert users
sq.MustExecParams("INSERT INTO users (id, name) VALUES (?, ?)", 3, 2, []sqinn.Value{
sqinn.Int32Value(1), sqinn.StringValue("Alice"),
sqinn.Int32Value(2), sqinn.StringValue("Bob"),
sqinn.Int32Value(3), sqinn.StringValue("Carol"),
})
// Query users
rows := sq.MustQueryRows(
"SELECT id, name FROM users WHERE id >= ? ORDER BY id",
[]sqinn.Value{sqinn.Int32Value(0)}, // query parameters
[]byte{sqinn.ValInt32, sqinn.ValString}, // fetch id as int, name as string
)
for _, values := range rows {
fmt.Printf("user id=%d, name=%s\n", values[0].Int32, values[1].String)
}
}