2024-10-15 14:42:56 +01:00
# Go bindings to SQLite using wazero
2023-01-12 16:29:57 +00:00
2023-01-26 01:11:09 +00:00
[](https://pkg.go.dev/github.com/ncruces/go-sqlite3)
[](https://goreportcard.com/report/github.com/ncruces/go-sqlite3)
2023-03-01 13:27:50 +00:00
[](https://github.com/ncruces/go-sqlite3/wiki/Test-coverage-report)
2023-01-12 16:29:57 +00:00
2024-04-16 14:02:23 +01:00
Go module `github.com/ncruces/go-sqlite3` is a `cgo` -free [SQLite ](https://sqlite.org/ ) wrapper.\
2023-12-15 10:46:29 +00:00
It provides a [`database/sql` ](https://pkg.go.dev/database/sql ) compatible driver,
as well as direct access to most of the [C SQLite API ](https://sqlite.org/cintro.html ).
2024-04-18 01:39:47 +01:00
It wraps a [Wasm ](https://webassembly.org/ ) [build ](embed/ ) of SQLite,
and uses [wazero ](https://wazero.io/ ) as the runtime.\
2024-12-11 15:05:22 +00:00
Go, wazero and [`x/sys` ](https://pkg.go.dev/golang.org/x/sys ) are the _ only _ direct dependencies.
2023-12-15 10:46:29 +00:00
2024-08-15 12:42:38 +01:00
### Getting started
Using the [`database/sql` ](https://pkg.go.dev/database/sql ) driver:
```go
import "database/sql"
import _ "github.com/ncruces/go-sqlite3/driver"
import _ "github.com/ncruces/go-sqlite3/embed"
var version string
db, _ := sql.Open("sqlite3", "file:demo.db")
db.QueryRow(`SELECT sqlite_version()` ).Scan(&version)
```
2023-12-15 10:46:29 +00:00
### Packages
2023-02-24 14:31:41 +00:00
2023-09-15 15:37:57 +01:00
- [`github.com/ncruces/go-sqlite3` ](https://pkg.go.dev/github.com/ncruces/go-sqlite3 )
2023-11-09 16:35:45 +00:00
wraps the [C SQLite API ](https://sqlite.org/cintro.html )
2025-05-26 12:01:48 +01:00
([example ](https://pkg.go.dev/github.com/ncruces/go-sqlite3#example-package )).
2023-09-15 15:37:57 +01:00
- [`github.com/ncruces/go-sqlite3/driver` ](https://pkg.go.dev/github.com/ncruces/go-sqlite3/driver )
2023-09-01 15:54:40 +01:00
provides a [`database/sql` ](https://pkg.go.dev/database/sql ) driver
2025-05-26 12:01:48 +01:00
([example ](https://pkg.go.dev/github.com/ncruces/go-sqlite3/driver#example-package )).
2023-09-15 15:37:57 +01:00
- [`github.com/ncruces/go-sqlite3/embed` ](https://pkg.go.dev/github.com/ncruces/go-sqlite3/embed )
2023-09-01 15:54:40 +01:00
embeds a build of SQLite into your application.
2023-11-21 13:40:55 +00:00
- [`github.com/ncruces/go-sqlite3/vfs` ](https://pkg.go.dev/github.com/ncruces/go-sqlite3/vfs )
wraps the [C SQLite VFS API ](https://sqlite.org/vfs.html ) and provides a pure Go implementation.
- [`github.com/ncruces/go-sqlite3/gormlite` ](https://pkg.go.dev/github.com/ncruces/go-sqlite3/gormlite )
provides a [GORM ](https://gorm.io ) driver.
### Advanced features
2025-05-26 12:01:48 +01:00
- [incremental BLOB I/O ](https://sqlite.org/c3ref/blob_open.html )
([example ](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/blobio#example-package ))
2023-12-30 00:47:16 +00:00
- [nested transactions ](https://sqlite.org/lang_savepoint.html )
2025-05-26 12:01:48 +01:00
([example ](https://pkg.go.dev/github.com/ncruces/go-sqlite3/driver#example-Savepoint ))
2023-12-30 00:47:16 +00:00
- [custom functions ](https://sqlite.org/c3ref/create_function.html )
2025-05-26 12:01:48 +01:00
([example ](https://pkg.go.dev/github.com/ncruces/go-sqlite3#example-Conn.CreateFunction ))
2023-12-30 00:47:16 +00:00
- [virtual tables ](https://sqlite.org/vtab.html )
2025-05-26 12:01:48 +01:00
([example ](https://pkg.go.dev/github.com/ncruces/go-sqlite3#example-CreateModule ))
- [custom VFSes ](https://sqlite.org/vfs.html )
([examples ](vfs/README.md#custom-vfses ))
2023-12-30 00:47:16 +00:00
- [online backup ](https://sqlite.org/backup.html )
2025-05-26 12:01:48 +01:00
([example ](https://pkg.go.dev/github.com/ncruces/go-sqlite3/driver#Conn ))
2023-12-30 00:47:16 +00:00
- [JSON support ](https://sqlite.org/json1.html )
2025-05-26 12:01:48 +01:00
([example ](https://pkg.go.dev/github.com/ncruces/go-sqlite3/driver#example-package-Json ))
2023-12-30 00:47:16 +00:00
- [math functions ](https://sqlite.org/lang_mathfunc.html )
- [full-text search ](https://sqlite.org/fts5.html )
- [geospatial search ](https://sqlite.org/geopoly.html )
2024-10-15 14:42:56 +01:00
- [Unicode support ](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/unicode )
- [statistics functions ](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/stats )
2024-04-18 01:42:25 +01:00
- [encryption at rest ](vfs/adiantum/README.md )
2024-10-15 14:42:56 +01:00
- [many extensions ](ext/README.md )
2023-12-30 00:47:16 +00:00
- [and more… ](embed/README.md )
2023-02-24 14:31:41 +00:00
### Caveats
2023-11-09 16:35:45 +00:00
This module replaces the SQLite [OS Interface ](https://sqlite.org/vfs.html )
2024-04-11 13:20:30 +01:00
(aka VFS) with a [pure Go ](vfs/ ) implementation,
which has advantages and disadvantages.
Read more about the Go VFS design [here ](vfs/README.md ).
2023-09-01 15:54:40 +01:00
2025-03-07 00:38:26 +00:00
Because each database connection executes within a Wasm sandboxed environment,
memory usage will be higher than alternatives.
2023-12-30 00:47:16 +00:00
### Testing
This project aims for [high test coverage ](https://github.com/ncruces/go-sqlite3/wiki/Test-coverage-report ).
2024-01-18 15:53:00 +00:00
It also benefits greatly from [SQLite's ](https://sqlite.org/testing.html ) and
2025-03-07 00:38:26 +00:00
[wazero's ](https://tetrate.io/blog/introducing-wazero-from-tetrate/#:~:text=Rock%2Dsolid%20test%20approach )
thorough testing.
2023-03-10 16:25:07 +00:00
2025-05-30 15:29:34 +02:00
Every commit is tested on:
2025-11-10 11:26:28 +00:00
* Linux: amd64, arm64, 386, arm, riscv64, ppc64le, loong64, s390x
2025-05-30 15:29:34 +02:00
* macOS: amd64, arm64
2025-11-09 12:41:32 +00:00
* Windows: amd64, arm64
2025-05-30 15:29:34 +02:00
* BSD:
* FreeBSD: amd64, arm64
* NetBSD: amd64, arm64
* DragonFly BSD: amd64
2025-11-09 12:41:32 +00:00
* OpenBSD: amd64
2025-05-30 15:29:34 +02:00
* illumos: amd64
* Solaris: amd64
Certain operating system and CPU combinations have some limitations. See the [support matrix ](https://github.com/ncruces/go-sqlite3/wiki/Support-matrix ) for a complete overview.
2024-05-05 23:18:58 +01:00
2024-04-11 13:20:30 +01:00
The Go VFS is tested by running SQLite's
2024-04-24 15:49:45 +01:00
[mptest ](https://github.com/sqlite/sqlite/blob/master/mptest/mptest.c ).
2023-12-30 00:47:16 +00:00
### Performance
2025-03-07 00:38:26 +00:00
Performance of the [`database/sql` ](https://pkg.go.dev/database/sql ) driver is
2023-12-30 00:47:16 +00:00
[competitive ](https://github.com/cvilsmeier/go-sqlite-bench ) with alternatives.
2025-03-07 00:38:26 +00:00
The Wasm and VFS layers are also benchmarked by running SQLite's
2023-03-18 03:03:11 +00:00
[speedtest1 ](https://github.com/sqlite/sqlite/blob/master/test/speedtest1.c ).
2025-03-07 00:38:26 +00:00
### Concurrency
This module behaves similarly to SQLite in [multi-thread ](https://sqlite.org/threadsafe.html ) mode:
it is goroutine-safe, provided that no single database connection, or object derived from it,
is used concurrently by multiple goroutines.
2025-03-13 23:38:50 +00:00
The [`database/sql` ](https://pkg.go.dev/database/sql ) API is safe to use concurrently,
2025-03-07 00:38:26 +00:00
according to its documentation.
2024-11-25 14:17:08 +00:00
### FAQ, issues, new features
For questions, please see [Discussions ](https://github.com/ncruces/go-sqlite3/discussions/categories/q-a ).
Also, post there if you used this driver for something interesting
([_"Show and tell"_ ](https://github.com/ncruces/go-sqlite3/discussions/categories/show-and-tell )),
have an [idea ](https://github.com/ncruces/go-sqlite3/discussions/categories/ideas )…
2025-02-27 23:49:27 +00:00
The [Issue ](https://github.com/ncruces/go-sqlite3/issues ) tracker is for bugs,
2024-11-25 14:17:08 +00:00
and features we're working on, planning to work on, or asking for help with.
2023-02-24 14:31:41 +00:00
### Alternatives
- [`modernc.org/sqlite` ](https://pkg.go.dev/modernc.org/sqlite )
- [`crawshaw.io/sqlite` ](https://pkg.go.dev/crawshaw.io/sqlite )
2023-02-26 03:22:08 +00:00
- [`github.com/mattn/go-sqlite3` ](https://pkg.go.dev/github.com/mattn/go-sqlite3 )
2025-05-26 12:01:48 +01:00
- [`github.com/zombiezen/go-sqlite` ](https://pkg.go.dev/github.com/zombiezen/go-sqlite )