libsql-client-go

module
v0.0.0-...-1956c2a Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2024 License: MIT

README

Go SDK for libSQL

License

This module implements a libSQL driver for the standard Go database/sql package. You can use it to interact with the following types of databases:

Installation

Install the driver into your module:

go get github.com/tursodatabase/libsql-client-go

Import the driver into your code using a blank import:

import (
	_ "github.com/tursodatabase/libsql-client-go/libsql"
)

Ensure all the module requirements are up to date:

go mod tidy
Add support for sqlite3 database files

To use a sqlite3 database file, you must also install and import one of the following SQLite drivers:

This enables the use of file: URLs with this driver.

Open a connection to sqld

Specify the "libsql" driver and a database URL in your call to sql.Open:

import (
	"database/sql"
	"fmt"
	"os"

	_ "github.com/tursodatabase/libsql-client-go/libsql"
)

var dbUrl = "http://127.0.0.1:8080"
db, err := sql.Open("libsql", dbUrl)
if err != nil {
    fmt.Fprintf(os.Stderr, "failed to open db %s: %s", dbUrl, err)
    os.Exit(1)
}

If your sqld instance is managed by Turso, the database URL must contain a valid database auth token in the query string:

var dbUrl = "libsql://[your-database].turso.io?authToken=[your-auth-token]"

Open a connection to a local sqlite3 database file

You can use a file: URL to locate a sqlite3 database file for use with this driver. The example below assumes that the package modernc.org/sqlite is installed:

import (
	"database/sql"
	"fmt"
	"os"

	_ "github.com/tursodatabase/libsql-client-go/libsql"
	_ "modernc.org/sqlite"
)

var dbUrl = "file:path/to/file.db"
db, err := sql.Open("libsql", dbUrl)
if err != nil {
    fmt.Fprintf(os.Stderr, "failed to open db %s: %s", dbUrl, err)
    os.Exit(1)
}

Compatibility with database/sql

This driver currently does not support prepared statements using db.Prepare() when querying sqld over HTTP.

License

This project is licensed under the MIT license.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in sqld by you, shall be licensed as MIT, without any additional terms or conditions.

Directories

Path Synopsis
sql_driver module

Jump to

Keyboard shortcuts

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