Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
98 commits
Select commit Hold shift + click to select a range
edc3bb6
Fix virtual table example. (#1149)
andrzh Apr 6, 2023
d366d8d
Update README.md
parthokr May 16, 2023
f08f1b6
Update amalgamation code
mattn May 31, 2023
9fd6f4f
Update amalgamation code
mattn Nov 4, 2023
c260ecf
use locally checked out lib in the docker job
itizir Dec 13, 2023
d9e2789
temporary fix to make the job pass
itizir Dec 13, 2023
00b02e0
Fix musl build (#1164)
leso-kn Dec 8, 2023
c91bca4
update go version to 1.19
mattn Jan 25, 2024
1f0dc0a
go fmt ./...
mattn Jan 25, 2024
d3c3333
update go version
mattn Jan 25, 2024
cdc8095
Update amalgamation code to 3.45.0
mattn Jan 25, 2024
64bbe62
add example
mattn Jan 25, 2024
4702d9b
Update amalgamation code to 3.45.1
mattn Jan 31, 2024
6ee3e67
close channel
mattn Feb 2, 2024
18cdded
fix: some typos
pomadev Feb 21, 2024
3c0390b
add support for libsqlite3 on z/OS
dustin-ward Apr 30, 2024
846fea6
Update amalgamation code to 3.46.1
mattn Sep 4, 2024
82bc911
close statement when missing query arguments
mattn Oct 4, 2024
348128f
Upgrade upload-artifact action
jonstacks Nov 26, 2024
ab13d63
Remove suggestion that CGO isn't always needed (#1290)
samjewell Dec 5, 2024
c61eeb5
remove superfluous use of runtime.SetFinalizer on SQLiteRows
charlievieth Dec 8, 2024
7658c06
Fix sqlite3_opt_unlock_notify with USE_LIBSQLITE3 (#1262)
q66 Dec 9, 2024
cff37b4
Fix json example
Jaculabilis Jan 21, 2025
b9f4d8c
chore: Fix memory leak in callbackRetText function
hionay Jul 12, 2024
9794660
docs: clarify GCP section
justinsb Dec 13, 2024
8d69329
revert #1259
mattn Apr 2, 2025
65302b0
Add ability to set an int64 file control (#1298)
jonstacks Apr 5, 2025
f76bae4
upgrade amalgamation code
mattn Apr 16, 2025
8c283ed
Update amalgamation code to 3.50.3
mattn Jul 24, 2025
d8fd268
drop userauth implementation
mattn Jul 26, 2025
90f0aff
drop userauth feature
mattn Jul 26, 2025
5629687
update GitHub workflows
mattn Jul 26, 2025
37998ab
drop tests for userauth
mattn Jul 26, 2025
a8be3f8
fix GitHub workflow
mattn Jul 26, 2025
4c1ac1e
fix GitHub workflow
mattn Jul 26, 2025
69a843e
fix GitHub workflow
mattn Jul 26, 2025
4372bf2
enable test for sqlite_userauth but it is dropped at least
mattn Jul 26, 2025
eb86f08
check authEnabled
mattn Jul 26, 2025
c857a95
drop call of AuthEnabled()
mattn Jul 28, 2025
a9775d4
return error
mattn Jul 30, 2025
e63a0c3
fix test
mattn Jul 30, 2025
2e165e0
fix syntax error
eraytufan Aug 11, 2025
8bf7a8a
update amalgamation code
mattn Aug 14, 2025
a66908a
use quote include instead of angled include for sqlite3-binding.h
nautaa Nov 10, 2025
aa7cdd3
update upgrade script
mattn Jan 1, 2026
2f6a2bb
fix upgrade script
mattn Jan 1, 2026
3e773a9
update github workflows
mattn Jan 1, 2026
3c885a9
Upgrade SQLite to version 3051001
mattn Jan 1, 2026
dce6b34
Add percentile extension
Dec 9, 2025
a510883
Upgrade SQLite to version 3051002
mattn Jan 16, 2026
2087331
add script to create pull-request
mattn Jan 16, 2026
8366a00
create pull-request
mattn Mar 4, 2026
5a1f4d3
use unsafe.Slice
mattn Mar 4, 2026
8c99a68
Call sqlite3_clear_bindings() in bind() to reset parameters
mattn Mar 16, 2026
4ea2a9f
Upgrade SQLite to version 3051003
mattn Mar 16, 2026
d71eda8
Fix upgrade.sh: add already-up-to-date check and fix changelog URL fo…
mattn Mar 16, 2026
0f12d4e
Ensure Close always removes runtime finalizer to prevent memory leak
mattn Mar 16, 2026
bc7436e
Bump GitHub Actions versions to latest
mattn Mar 16, 2026
bb8d0b2
Bump Go test matrix versions from 1.23-1.25 to 1.24-1.26
mattn Mar 16, 2026
57e5007
Merge pull request #1313 from Jaculabilis/json-example
mattn Mar 16, 2026
84bdc43
add missing index constraint op types
theimpostor Apr 27, 2025
0d23881
Merge pull request #1379 from theimpostor/pr-1322-missing-constraint-…
mattn Mar 19, 2026
8f9f86e
Eliminate unnecessary bounds checks in hot paths
mattn Mar 30, 2026
edadafa
Merge pull request #1381 from mattn/eliminate-bounds-checks
mattn Mar 30, 2026
9a908a9
optimize sqlite bind fast path
mattn Apr 3, 2026
cc39db7
Merge pull request #1382 from mattn/codex/sqlite3-bind-fastpath
mattn Apr 3, 2026
e1557be
batch row column fetches in Next
mattn Apr 3, 2026
b23d54c
Merge pull request #1383 from mattn/codex/next-row-batch-fetch
mattn Apr 3, 2026
efae5e7
raise minimum Go version to 1.21
mattn Apr 3, 2026
f929738
Merge pull request #1384 from mattn/raise-go121-cleanup
mattn Apr 3, 2026
61c5b98
reduce sqlite bind overhead
mattn Apr 6, 2026
4a311ff
Merge pull request #1385 from mattn/perf/reduce-bind-overhead
mattn Apr 6, 2026
4954048
reduce CGO call overhead for exec and bind paths
mattn Apr 6, 2026
89f4bbe
fix build with SQLITE_ENABLE_UNLOCK_NOTIFY
mattn Apr 6, 2026
8d12439
Merge pull request #1386 from mattn/perf/reduce-cgo-overhead
mattn Apr 6, 2026
efa9b1c
add opt-in statement cache
mattn Apr 7, 2026
061c2a5
check stmtCacheSize before acquiring mutex in takeCachedStmt
mattn Apr 8, 2026
325cb8d
remove redundant stmtCacheSize check in putCachedStmt
mattn Apr 8, 2026
e9f47da
do not bail out on finalize error in closeCachedStmtsLocked
mattn Apr 8, 2026
0e58fa4
simplify prepareWithCache to call prepare instead of duplicating logic
mattn Apr 8, 2026
867dcbf
move reset/clear into putCachedStmt and always finalize on failure
mattn Apr 8, 2026
e302e5c
document that _stmt_cache_size is per connection
mattn Apr 8, 2026
5df13a0
Merge pull request #1387 from mattn/codex/stmt-cache
mattn Apr 8, 2026
1627011
add _stmt_cache_size to DSN parameters table in README
mattn Apr 8, 2026
58e032d
Revise SECURITY.md for version support and reporting
mattn Apr 10, 2026
7716c20
evict LRU stmt when stmt cache is full
mattn Apr 11, 2026
2badb4c
use slice len/cap for stmt cache instead of separate counters
mattn Apr 14, 2026
59e8e75
only set stmt cacheKey when cache is enabled
mattn Apr 14, 2026
6690238
extract finalizeCachedStmt helper and drop redundant tail reset
mattn Apr 14, 2026
d8e8642
SQLite3MultipleCiphers support (2.2.7)
Apr 25, 2026
ce23c50
Merge remote-tracking branch 'mattn/master' into sqlite3mc-2.3.3
Apr 25, 2026
869e516
fix panic when querying input with no SQL (only comments/whitespace)
mattn Apr 29, 2026
c719e20
Merge pull request #1392 from mattn/fix-issue-1390-query-comment-panic
mattn Apr 29, 2026
1aa7317
Merge pull request #1388 from mattn/stmt-cache-lru
mattn Apr 29, 2026
3761cf7
Upgrade SQLite to version 3053000
mattn Apr 29, 2026
2d4d220
fix changelog URL when minor or patch version is zero
mattn Apr 29, 2026
20826e8
Merge pull request #1394 from mattn/sqlite-amalgamation-3053000
mattn Apr 29, 2026
ef9115b
Merge mattn/go-sqlite3 master (v1.14.44)
May 9, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/cifuzz.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
dry-run: false
sanitizer: ${{ matrix.sanitizer }}
- name: Upload Crash
uses: actions/upload-artifact@v1
uses: actions/upload-artifact@v4
if: failure()
with:
name: ${{ matrix.sanitizer }}-artifacts
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ jobs:
name: Run Dockerfiles in examples
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v6

- name: Run example - simple
run: |
cd ./_example/simple
docker build -t simple .
docker build -t simple -f ./_example/simple/Dockerfile .
docker run simple | grep 99\ こんにちは世界099
20 changes: 10 additions & 10 deletions .github/workflows/go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
go: ['1.18', '1.19', '1.20']
go: ['1.24', '1.25', '1.26']
fail-fast: false
env:
OS: ${{ matrix.os }}
Expand All @@ -23,7 +23,7 @@ jobs:
- if: startsWith(matrix.os, 'macos')
run: brew update

- uses: actions/setup-go@v2
- uses: actions/setup-go@v6
with:
go-version: ${{ matrix.go }}

Expand All @@ -35,7 +35,7 @@ jobs:
run: |
echo "$(go env GOPATH)/bin" >> "$GITHUB_PATH"

- uses: actions/checkout@v2
- uses: actions/checkout@v6

- name: 'Tags: default'
run: go-acc . -- -race -v -tags ""
Expand All @@ -44,13 +44,13 @@ jobs:
run: go-acc . -- -race -v -tags "libsqlite3"

- name: 'Tags: full'
run: go-acc . -- -race -v -tags "sqlite_allow_uri_authority sqlite_app_armor sqlite_column_metadata sqlite_foreign_keys sqlite_fts5 sqlite_icu sqlite_introspect sqlite_json sqlite_math_functions sqlite_os_trace sqlite_preupdate_hook sqlite_secure_delete sqlite_see sqlite_stat4 sqlite_trace sqlite_unlock_notify sqlite_userauth sqlite_vacuum_incr sqlite_vtable"
run: go-acc . -- -race -v -tags "sqlite_allow_uri_authority sqlite_app_armor sqlite_column_metadata sqlite_foreign_keys sqlite_fts5 sqlite_icu sqlite_introspect sqlite_json sqlite_math_functions sqlite_os_trace sqlite_preupdate_hook sqlite_secure_delete sqlite_see sqlite_stat4 sqlite_trace sqlite_unlock_notify sqlite_vacuum_incr sqlite_vtable"

- name: 'Tags: vacuum'
run: go-acc . -- -race -v -tags "sqlite_vacuum_full"

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
uses: codecov/codecov-action@v5
with:
env_vars: OS,GO
file: coverage.txt
Expand All @@ -64,7 +64,7 @@ jobs:

strategy:
matrix:
go: ['1.18', '1.19', '1.20']
go: ['1.24', '1.25', '1.26']
fail-fast: false
env:
OS: windows-latest
Expand All @@ -77,7 +77,7 @@ jobs:
msystem: MINGW64
path-type: inherit

- uses: actions/setup-go@v2
- uses: actions/setup-go@v6
with:
go-version: ${{ matrix.go }}

Expand All @@ -86,7 +86,7 @@ jobs:
echo "$(go env GOPATH)/bin" >> "$GITHUB_PATH"
shell: msys2 {0}

- uses: actions/checkout@v2
- uses: actions/checkout@v6

- name: 'Tags: default'
run: go build -race -v -tags ""
Expand All @@ -99,15 +99,15 @@ jobs:
- name: 'Tags: full'
run: |
echo 'skip this test'
echo go build -race -v -tags "sqlite_allow_uri_authority sqlite_app_armor sqlite_column_metadata sqlite_foreign_keys sqlite_fts5 sqlite_icu sqlite_introspect sqlite_json sqlite_math_functions sqlite_preupdate_hook sqlite_secure_delete sqlite_see sqlite_stat4 sqlite_trace sqlite_unlock_notify sqlite_userauth sqlite_vacuum_incr sqlite_vtable"
echo go build -race -v -tags "sqlite_allow_uri_authority sqlite_app_armor sqlite_column_metadata sqlite_foreign_keys sqlite_fts5 sqlite_icu sqlite_introspect sqlite_json sqlite_math_functions sqlite_preupdate_hook sqlite_secure_delete sqlite_see sqlite_stat4 sqlite_trace sqlite_unlock_notify sqlite_vacuum_incr sqlite_vtable"
shell: msys2 {0}

- name: 'Tags: vacuum'
run: go build -race -v -tags "sqlite_vacuum_full"
shell: msys2 {0}

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v2
uses: codecov/codecov-action@v5
with:
env_vars: OS,GO
file: coverage.txt
Expand Down
39 changes: 28 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,19 @@ This package follows the official [Golang Release Policy](https://golang.org/doc
- [Android](#android)
- [ARM](#arm)
- [Cross Compile](#cross-compile)
- [Google Cloud Platform](#google-cloud-platform)
- [Compiling](#compiling)
- [Linux](#linux)
- [Alpine](#alpine)
- [Fedora](#fedora)
- [Ubuntu](#ubuntu)
- [macOS](#mac-osx)
- [Windows](#windows)
- [Errors](#errors)
- [User Authentication](#user-authentication)
- [Compile](#compile)
- [Encryption](#encryption)
- [Usage](#usage-1)
- [User Authentication](#user-authentication)
- [Compile](#compile-1)
- [Usage](#usage-2)
- [Create protected database](#create-protected-database)
- [Password Encoding](#password-encoding)
- [Available Encoders](#available-encoders)
Expand All @@ -70,9 +72,8 @@ This package can be installed with the `go get` command:

_go-sqlite3_ is *cgo* package.
If you want to build your app using go-sqlite3, you need gcc.
However, after you have built and installed _go-sqlite3_ with `go install github.com/mattn/go-sqlite3` (which requires gcc), you can build your app without relying on gcc in future.

***Important: because this is a `CGO` enabled package, you are required to set the environment variable `CGO_ENABLED=1` and have a `gcc` compile present within your path.***
***Important: because this is a `CGO` enabled package, you are required to set the environment variable `CGO_ENABLED=1` and have a `gcc` compiler present within your path.***

# API Reference

Expand Down Expand Up @@ -109,12 +110,21 @@ Boolean values can be one of:
| Auto Vacuum | `_auto_vacuum` \| `_vacuum` | <ul><li>`0` \| `none`</li><li>`1` \| `full`</li><li>`2` \| `incremental`</li></ul> | For more information see [PRAGMA auto_vacuum](https://www.sqlite.org/pragma.html#pragma_auto_vacuum) |
| Busy Timeout | `_busy_timeout` \| `_timeout` | `int` | Specify value for sqlite3_busy_timeout. For more information see [PRAGMA busy_timeout](https://www.sqlite.org/pragma.html#pragma_busy_timeout) |
| Case Sensitive LIKE | `_case_sensitive_like` \| `_cslike` | `boolean` | For more information see [PRAGMA case_sensitive_like](https://www.sqlite.org/pragma.html#pragma_case_sensitive_like) |
| Cipher | `_cipher` | `string` | Selects the cipher to be used for encrypting the database, for more information see [PRAGMA cipher](https://utelle.github.io/SQLite3MultipleCiphers/docs/configuration/config_sql_pragmas/#pragma-cipher) |
| Defer Foreign Keys | `_defer_foreign_keys` \| `_defer_fk` | `boolean` | For more information see [PRAGMA defer_foreign_keys](https://www.sqlite.org/pragma.html#pragma_defer_foreign_keys) |
| Foreign Keys | `_foreign_keys` \| `_fk` | `boolean` | For more information see [PRAGMA foreign_keys](https://www.sqlite.org/pragma.html#pragma_foreign_keys) |
| Check HMAC | `_hmac_check` | `boolean` | Selects whether the HMAC should be validated on read operations for encryption schemes using HMACs, for more information see [PRAGMA hmac_check](https://utelle.github.io/SQLite3MultipleCiphers/docs/configuration/config_sql_pragmas/#pragma-hmac_check) |
| Use HMAC | `_hmac_use` | `boolean` | Enable or disable the use of per-page HMACs for the cipher scheme SQLCipher, for more information see [PRAGMA hmac_use](https://utelle.github.io/SQLite3MultipleCiphers/docs/configuration/config_sql_pragmas/#pragma-hmac_use) |
| Ignore CHECK Constraints | `_ignore_check_constraints` | `boolean` | For more information see [PRAGMA ignore_check_constraints](https://www.sqlite.org/pragma.html#pragma_ignore_check_constraints) |
| Immutable | `immutable` | `boolean` | For more information see [Immutable](https://www.sqlite.org/c3ref/open.html) |
| Journal Mode | `_journal_mode` \| `_journal` | <ul><li>DELETE</li><li>TRUNCATE</li><li>PERSIST</li><li>MEMORY</li><li>WAL</li><li>OFF</li></ul> | For more information see [PRAGMA journal_mode](https://www.sqlite.org/pragma.html#pragma_journal_mode) |
| KDF iterations | `_kdf_iter` | `int` | Specifies the number of iterations used for key derivation, for more information see [PRAGMA kdf_iter](https://utelle.github.io/SQLite3MultipleCiphers/docs/configuration/config_sql_pragmas/#pragma-kdf_iter) |
| Encryption key | `_key` | `string` | Sets the database encryption key, for more information see [PRAGMA key](https://utelle.github.io/SQLite3MultipleCiphers/docs/configuration/config_sql_pragmas/#pragma-key) |
| Legacy | `_legacy` | `int` | Defines the legacy mode for a cipher scheme, for more information see [PRAGMA legacy](https://utelle.github.io/SQLite3MultipleCiphers/docs/configuration/config_sql_pragmas/#pragma-legacy) |
| Legacy page size | `_legacy_page_size` | `int` | Specifies the database page size to be used in legacy mode for a cipher scheme, for more information see [PRAGMA legacy_page_size](https://utelle.github.io/SQLite3MultipleCiphers/docs/configuration/config_sql_pragmas/#pragma-legacy_page_size) |
| Locking Mode | `_locking_mode` \| `_locking` | <ul><li>NORMAL</li><li>EXCLUSIVE</li></ul> | For more information see [PRAGMA locking_mode](https://www.sqlite.org/pragma.html#pragma_locking_mode) |
| MC Legacy WAL | `_mc_legacy_wal` | `boolean` | Selects whether the legacy mode for the WAL journal encryption should be used, for more information see [PRAGMA mc_legacy_wal](https://utelle.github.io/SQLite3MultipleCiphers/docs/configuration/config_sql_pragmas/#pragma-mc_legacy_wal) |
| Plaintext header size | `_plaintext_header_size` | `int` | Allocates a portion of the database header which will not be encrypted to allow identification as an SQLite database, for more information see [PRAGMA plaintext_header_size](https://utelle.github.io/SQLite3MultipleCiphers/docs/configuration/config_sql_pragmas/#pragma-plaintext_header_size) |
| Mode | `mode` | <ul><li>ro</li><li>rw</li><li>rwc</li><li>memory</li></ul> | Access Mode of the database. For more information see [SQLite Open](https://www.sqlite.org/c3ref/open.html) |
| Mutex Locking | `_mutex` | <ul><li>no</li><li>full</li></ul> | Specify mutex mode. |
| Query Only | `_query_only` | `boolean` | For more information see [PRAGMA query_only](https://www.sqlite.org/pragma.html#pragma_query_only) |
Expand All @@ -126,6 +136,7 @@ Boolean values can be one of:
| Transaction Lock | `_txlock` | <ul><li>immediate</li><li>deferred</li><li>exclusive</li></ul> | Specify locking behavior for transactions. |
| Writable Schema | `_writable_schema` | `Boolean` | When this pragma is on, the SQLITE_MASTER tables in which database can be changed using ordinary UPDATE, INSERT, and DELETE statements. Warning: misuse of this pragma can easily result in a corrupt database file. |
| Cache Size | `_cache_size` | `int` | Maximum cache size; default is 2000K (2M). See [PRAGMA cache_size](https://sqlite.org/pragma.html#pragma_cache_size) |
| Statement Cache Size | `_stmt_cache_size` | `int` | Maximum number of prepared statements cached per connection; default is 0 (disabled). Note that `sql.DB` is a connection pool, so each connection maintains its own independent cache. |


## DSN Examples
Expand Down Expand Up @@ -175,6 +186,7 @@ go build -tags "icu json1 fts5 secure_delete"
| JSON SQL Functions | sqlite_json | When this option is defined in the amalgamation, the JSON SQL functions are added to the build automatically |
| Math Functions | sqlite_math_functions | This compile-time option enables built-in scalar math functions. For more information see [Built-In Mathematical SQL Functions](https://www.sqlite.org/lang_mathfunc.html) |
| OS Trace | sqlite_os_trace | This option enables OSTRACE() debug logging. This can be verbose and should not be used in production. |
| Percentile | sqlite_percentile | This option enables [The Percentile Extension](sqlite.org/percentile.html). |
| Pre Update Hook | sqlite_preupdate_hook | Registers a callback function that is invoked prior to each INSERT, UPDATE, and DELETE operation on a database table. |
| Secure Delete | sqlite_secure_delete | This compile-time option changes the default setting of the secure_delete pragma.<br><br>When this option is not used, secure_delete defaults to off. When this option is present, secure_delete defaults to on.<br><br>The secure_delete setting causes deleted content to be overwritten with zeros. There is a small performance penalty since additional I/O must occur.<br><br>On the other hand, secure_delete can prevent fragments of sensitive information from lingering in unused parts of the database file after it has been deleted. See the documentation on the secure_delete pragma for additional information |
| Secure Delete (FAST) | sqlite_secure_delete_fast | For more information see [PRAGMA secure_delete](https://www.sqlite.org/pragma.html#pragma_secure_delete) |
Expand Down Expand Up @@ -228,11 +240,7 @@ Steps:

Please refer to the project's [README](https://github.com/FiloSottile/homebrew-musl-cross#readme) for further information.

# Google Cloud Platform

Building on GCP is not possible because Google Cloud Platform does not allow `gcc` to be executed.

Please work only with compiled final binaries.
# Compiling

## Linux

Expand Down Expand Up @@ -354,8 +362,17 @@ For example the TDM-GCC Toolchain can be found [here](https://jmeubank.github.io
go install github.com/mattn/go-sqlite3
```

# Encryption

## Usage

Pass your encryption key via the `_key` argument in the connection string.
See the [SQLite3MultipleCiphers documentation](https://utelle.github.io/SQLite3MultipleCiphers/docs/configuration/config_sql_pragmas/#pragma-key) for more details.

# User Authentication

***This is deprecated***

This package supports the SQLite User Authentication module.

## Compile
Expand Down Expand Up @@ -594,7 +611,7 @@ sqlite3-binding.c, sqlite3-binding.h, sqlite3ext.h

The -binding suffix was added to avoid build failures under gccgo.

In this repository, those files are an amalgamation of code that was copied from SQLite3. The license of that code is the same as the license of SQLite3.
In this repository, those files are an amalgamation of code that was copied from SQLite3 and SQLite3MultipleCiphers. The license of that code is the same as the licenses of SQLite3 and SQLite3MultipleCiphers.

# Author

Expand Down
33 changes: 33 additions & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Security Policy

## Supported Versions

Only the latest release on the `v1.14.x` line receives security fixes.

| Version | Supported |
| -------- | ------------------ |
| 1.14.x | :white_check_mark: |
| < 1.14 | :x: |

## Scope

`go-sqlite3` is a CGo binding that bundles the SQLite amalgamation
(`sqlite3-binding.c` / `sqlite3-binding.h`). Please report issues to the
appropriate project:

- Bugs in the Go binding layer, CGo glue, build tags, or this repository's
own code: report here.
- Vulnerabilities in SQLite itself: please report them upstream to the
SQLite developers at <https://www.sqlite.org/>. Once a fix is released
upstream, this repository will update the bundled amalgamation.

## Reporting a Vulnerability

Please **do not** open a public GitHub issue for security problems.

Use GitHub's private vulnerability reporting:
<https://github.com/mattn/go-sqlite3/security/advisories/new>

This project is maintained on a best-effort basis by volunteers, so please
allow reasonable time for investigation and a fix before any public
d
129 changes: 129 additions & 0 deletions _example/json/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package main

import (
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
_ "github.com/mattn/go-sqlite3"
"log"
"os"
)

type Tag struct {
Name string `json:"name"`
Country string `json:"country"`
}

func (t *Tag) Scan(value interface{}) error {
return json.Unmarshal([]byte(value.(string)), t)
}

func (t *Tag) Value() (driver.Value, error) {
b, err := json.Marshal(t)
return string(b), err
}

func main() {
os.Remove("./foo.db")

db, err := sql.Open("sqlite3", "./foo.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()

// Using a json-typed column
// Verify type: `create table foo (tag text) strict`
_, err = db.Exec(`create table foo (tag json)`)
if err != nil {
log.Fatal(err)
}

stmt, err := db.Prepare("insert into foo(tag) values(?)")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
_, err = stmt.Exec(`{"name": "mattn", "country": "japan"}`)
if err != nil {
log.Fatal(err)
}
_, err = stmt.Exec(`{"name": "michael", "country": "usa"}`)
if err != nil {
log.Fatal(err)
}

var country string
err = db.QueryRow("select tag->>'country' from foo where tag->>'name' = 'mattn'").Scan(&country)
if err != nil {
log.Fatal(err)
}
fmt.Println(country)

var tag Tag
err = db.QueryRow("select tag from foo where tag->>'name' = 'mattn'").Scan(&tag)
if err != nil {
log.Fatal(err)
}

fmt.Println(tag.Name)

tag.Country = "日本"
_, err = db.Exec(`update foo set tag = ? where tag->>'name' == 'mattn'`, &tag)
if err != nil {
log.Fatal(err)
}

err = db.QueryRow("select tag->>'country' from foo where tag->>'name' = 'mattn'").Scan(&country)
if err != nil {
log.Fatal(err)
}
fmt.Println(country)

// Using a jsonb-typed column
// Verify type: `create table bar (tag blob) strict`
_, err = db.Exec(`create table bar (tag jsonb)`)
if err != nil {
log.Fatal(err)
}

stmt, err = db.Prepare("insert into bar(tag) values(jsonb(?))")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
_, err = stmt.Exec(`{"name": "mattn", "country": "japan"}`)
if err != nil {
log.Fatal(err)
}
_, err = stmt.Exec(`{"name": "michael", "country": "usa"}`)
if err != nil {
log.Fatal(err)
}

err = db.QueryRow("select tag->>'country' from bar where tag->>'name' = 'mattn'").Scan(&country)
if err != nil {
log.Fatal(err)
}
fmt.Println(country)

err = db.QueryRow("select json(tag) from bar where tag->>'name' = 'mattn'").Scan(&tag)
if err != nil {
log.Fatal(err)
}

fmt.Println(tag.Name)

tag.Country = "日本"
_, err = db.Exec(`update bar set tag = jsonb(?) where tag->>'name' == 'mattn'`, &tag)
if err != nil {
log.Fatal(err)
}

err = db.QueryRow("select tag->>'country' from bar where tag->>'name' = 'mattn'").Scan(&country)
if err != nil {
log.Fatal(err)
}
fmt.Println(country)
}
Loading