Skip to content

nsqlite/nsqlitego

Repository files navigation

SQLite Over The Network

Go Reference Go Report Card Release Version License

nsqlitego

NSQLite Go Driver, a driver for the NSQLite database engine — compatible with the standard database/sql package.

Features

  • Communicates with the NSQLite database engine over HTTP/HTTPS.
  • Implements database/sql/driver interfaces for seamless integration.
  • Supports transactions, prepared statements, and custom DSN parsing.
  • Zero dependencies outside the standard library.

Installation

go get github.com/nsqlite/nsqlitego

Ensure that you are using Go modules (go mod init) in your project.

Usage

Basic Example

Below is a concise example showing how to open a database and execute a simple query:

import (
  "database/sql"
  "fmt"
  _ "github.com/nsqlite/nsqlitego"
)

func main() {
  db, err := sql.Open("nsqlite", "http://localhost:9876?authToken=secret")
  if err != nil {
    panic(err)
  }
  defer db.Close()

  if err := db.Ping(); err != nil {
    panic("error pinging database: " + err.Error())
  }

  rows, err := db.Query("SELECT id, name FROM users")
  if err != nil {
    panic(err)
  }
  defer rows.Close()

  for rows.Next() {
    var id int
    var name string
    if err := rows.Scan(&id, &name); err != nil {
      panic(err)
    }
    fmt.Println(id, name)
  }
}

Transactions

Transactions are straightforward; they follow the standard pattern in database/sql:

tx, _ := db.Begin()
defer tx.Rollback()

_, _ = tx.Exec("INSERT INTO users(name) VALUES(?)", "Alice")
_, _ = tx.Exec("INSERT INTO users(name) VALUES(?)", "Bob")
_, _ = tx.Exec("INSERT INTO users(name) VALUES(?)", "Charlie")

if err := tx.Commit(); err != nil {
  // ...
}

Errors are ignored for brevity, but you should always handle them in your code.

Additional Packages

These packages are included in this repository, so no additional installation is required.

  • nsqlitedsn – Provides convenient parsing and manipulation of NSQLite connection strings.
  • nsqlitehttp – An alternative way to access the NSQLite database engine directly over HTTP, offering more granular control than the database/sql layer.

License

This project is licensed under the MIT license. See LICENSE for details.