Clone this repo:
  1. eed79b8 update README.md by Yasuhiro Matsumoto · 8 days ago master
  2. 1fc3fd3 Merge pull request #657 from kevinburke/compile-tip by mattn · 2 weeks ago
  3. 6a26e21 all: fix cgo compile failures on tip by Kevin Burke · 2 weeks ago
  4. c7c4067 Merge pull request #655 from typeless/fix-too-large-arrary-error-v2 by mattn · 3 weeks ago v1.10.0
  5. 576a2cb Fix build failure of incorrect expression for unsafe.Sizeof by Mura Li · 3 weeks ago

go-sqlite3

GoDoc Reference Build Status Coverage Status Go Report Card

Description

sqlite3 driver conforming to the built-in database/sql interface

Supported Golang version: See .travis.yml

This package follows the official Golang Release Policy.

Overview

Installation

This package can be installed with the go get command:

go get github.com/mattn/go-sqlite3

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.

API Reference

API documentation can be found here: http://godoc.org/github.com/mattn/go-sqlite3

Examples can be found under the examples directory

Connection String

When creating a new SQLite database or connection to an existing one, with the file name additional options can be given. This is also known as a DSN string. (Data Source Name).

Options are append after the filename of the SQLite database. The database filename and options are seperated by an ? (Question Mark). Options should be URL-encoded (see url.QueryEscape).

This also applies when using an in-memory database instead of a file.

Options can be given using the following format: KEYWORD=VALUE and multiple options can be combined with the & ampersand.

This library supports dsn options of SQLite itself and provides additional options.

Boolean values can be one of:

  • 0 no false off
  • 1 yes true on
NameKeyValue(s)Description
UA - Create_auth-Create User Authentication, for more information see User Authentication
UA - Username_auth_userstringUsername for User Authentication, for more information see User Authentication
UA - Password_auth_passstringPassword for User Authentication, for more information see User Authentication
UA - Crypt_auth_cryptSHA1SSHA1SHA256SSHA256SHA384SSHA384SHA512SSHA512Password encoder to use for User Authentication, for more information see User Authentication
UA - Salt_auth_saltstringSalt to use if the configure password encoder requires a salt, for User Authentication, for more information see User Authentication
Auto Vacuum_auto_vacuum | _vacuum0 | none1 | full2 | incrementalFor more information see PRAGMA auto_vacuum
Busy Timeout_busy_timeout | _timeoutintSpecify value for sqlite3_busy_timeout. For more information see PRAGMA busy_timeout
Case Sensitive LIKE_case_sensitive_like | _cslikebooleanFor more information see PRAGMA case_sensitive_like
Defer Foreign Keys_defer_foreign_keys | _defer_fkbooleanFor more information see PRAGMA defer_foreign_keys
Foreign Keys_foreign_keys | _fkbooleanFor more information see PRAGMA foreign_keys
Ignore CHECK Constraints_ignore_check_constraintsbooleanFor more information see PRAGMA ignore_check_constraints
ImmutableimmutablebooleanFor more information see Immutable
Journal Mode_journal_mode | _journalDELETETRUNCATEPERSISTMEMORYWALOFFFor more information see PRAGMA journal_mode
Locking Mode_locking_mode | _lockingNORMALEXCLUSIVEFor more information see PRAGMA locking_mode
ModemoderorwrwcmemoryAccess Mode of the database. For more information see SQLite Open
Mutex Locking_mutexnofullSpecify mutex mode.
Query Only_query_onlybooleanFor more information see PRAGMA query_only
Recursive Triggers_recursive_triggers | _rtbooleanFor more information see PRAGMA recursive_triggers
Secure Delete_secure_deleteboolean | FASTFor more information see PRAGMA secure_delete
Shared-Cache ModecachesharedprivateSet cache mode for more information see sqlite.org
Synchronous_synchronous | _sync0 | OFF1 | NORMAL2 | FULL3 | EXTRAFor more information see PRAGMA synchronous
Time Zone Location_locautoSpecify location of time format.
Transaction Lock_txlockimmediatedeferredexclusiveSpecify locking behavior for transactions.
Writable Schema_writable_schemaBooleanWhen 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.

DSN Examples

file:test.db?cache=shared&mode=memory

Features

This package allows additional configuration of features available within SQLite3 to be enabled or disabled by golang build constraints also known as build tags.

Click here for more information about build tags / constraints.

Usage

If you wish to build this library with additional extensions / features. Use the following command.

go build --tags "<FEATURE>"

For available features see the extension list. When using multiple build tags, all the different tags should be space delimted.

Example:

go build --tags "icu json1 fts5 secure_delete"

Feature / Extension List

ExtensionBuild TagDescription
Additional Statisticssqlite_stat4This option adds additional logic to the ANALYZE command and to the query planner that can help SQLite to chose a better query plan under certain situations. The ANALYZE command is enhanced to collect histogram data from all columns of every index and store that data in the sqlite_stat4 table.

The query planner will then use the histogram data to help it make better index choices. The downside of this compile-time option is that it violates the query planner stability guarantee making it more difficult to ensure consistent performance in mass-produced applications.

SQLITE_ENABLE_STAT4 is an enhancement of SQLITE_ENABLE_STAT3. STAT3 only recorded histogram data for the left-most column of each index whereas the STAT4 enhancement records histogram data from all columns of each index.

The SQLITE_ENABLE_STAT3 compile-time option is a no-op and is ignored if the SQLITE_ENABLE_STAT4 compile-time option is used
Allow URI Authoritysqlite_allow_uri_authorityURI filenames normally throws an error if the authority section is not either empty or “localhost”.

However, if SQLite is compiled with the SQLITE_ALLOW_URI_AUTHORITY compile-time option, then the URI is converted into a Uniform Naming Convention (UNC) filename and passed down to the underlying operating system that way
App Armorsqlite_app_armorWhen defined, this C-preprocessor macro activates extra code that attempts to detect misuse of the SQLite API, such as passing in NULL pointers to required parameters or using objects after they have been destroyed.

App Armor is not available under Windows.
Disable Load Extensionssqlite_omit_load_extensionLoading of external extensions is enabled by default.

To disable extension loading add the build tag sqlite_omit_load_extension.
Foreign Keyssqlite_foreign_keysThis macro determines whether enforcement of foreign key constraints is enabled or disabled by default for new database connections.

Each database connection can always turn enforcement of foreign key constraints on and off and run-time using the foreign_keys pragma.

Enforcement of foreign key constraints is normally off by default, but if this compile-time parameter is set to 1, enforcement of foreign key constraints will be on by default
Full Auto Vacuumsqlite_vacuum_fullSet the default auto vacuum to full
Incremental Auto Vacuumsqlite_vacuum_incrSet the default auto vacuum to incremental
Full Text Search Enginesqlite_fts5When this option is defined in the amalgamation, versions 5 of the full-text search engine (fts5) is added to the build automatically
International Components for Unicodesqlite_icuThis option causes the International Components for Unicode or “ICU” extension to SQLite to be added to the build
Introspect PRAGMASsqlite_introspectThis option adds some extra PRAGMA statements. PRAGMA function_listPRAGMA module_listPRAGMA pragma_list
JSON SQL Functionssqlite_jsonWhen this option is defined in the amalgamation, the JSON SQL functions are added to the build automatically
Secure Deletesqlite_secure_deleteThis compile-time option changes the default setting of the secure_delete pragma.

When this option is not used, secure_delete defaults to off. When this option is present, secure_delete defaults to on.

The secure_delete setting causes deleted content to be overwritten with zeros. There is a small performance penalty since additional I/O must occur.

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_fastFor more information see PRAGMA secure_delete
Tracing / Debugsqlite_traceActivate trace functions
User Authenticationsqlite_userauthSQLite User Authentication see User Authentication for more information.

Compilation

This package requires CGO_ENABLED=1 ennvironment variable if not set by default, and the presence of the gcc compiler.

If you need to add additional CFLAGS or LDFLAGS to the build command, and do not want to modify this package. Then this can be achieved by using the CGO_CFLAGS and CGO_LDFLAGS environment variables.

Android

This package can be compiled for android. Compile with:

go build --tags "android"

For more information see #201

ARM

To compile for ARM use the following environment.

env CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ \
    CGO_ENABLED=1 GOOS=linux GOARCH=arm GOARM=7 \
    go build -v 

Additional information:

Cross Compile

This library can be cross-compiled.

In some cases you are required to the CC environment variable with the cross compiler.

Additional 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.

Linux

To compile this package on Linux you must install the development tools for your linux distribution.

To compile under linux use the build tag linux.

go build --tags "linux"

If you wish to link directly to libsqlite3 then you can use the libsqlite3 build tag.

go build --tags "libsqlite3 linux"

Alpine

When building in an alpine container run the following command before building.

apk add --update gcc musl-dev

Fedora

sudo yum groupinstall "Development Tools" "Development Libraries"

Ubuntu

sudo apt-get install build-essential

Mac OSX

OSX should have all the tools present to compile this package, if not install XCode this will add all the developers tools.

Required dependency

brew install sqlite3

For OSX there is an additional package install which is required if you whish to build the icu extension.

This additional package can be installed with homebrew.

brew upgrade icu4c

To compile for Mac OSX.

go build --tags "darwin"

If you wish to link directly to libsqlite3 then you can use the libsqlite3 build tag.

go build --tags "libsqlite3 darwin"

Additional information:

Windows

To compile this package on Windows OS you must have the gcc compiler installed.

  1. Install a Windows gcc toolchain.
  2. Add the bin folders to the Windows path if the installer did not do this by default.
  3. Open a terminal for the TDM-GCC toolchain, can be found in the Windows Start menu.
  4. Navigate to your project folder and run the go build ... command for this package.

For example the TDM-GCC Toolchain can be found here.

Errors

  • Compile error: can not be used when making a shared object; recompile with -fPIC

    When receiving a compile time error referencing recompile with -FPIC then you are probably using a hardend system.

    You can compile the library on a hardend system with the following command.

    go build -ldflags '-extldflags=-fno-PIC'
    

    More details see #120

  • Can't build go-sqlite3 on windows 64bit.

    Probably, you are using go 1.0, go1.0 has a problem when it comes to compiling/linking on windows 64bit. See: #27

  • go get github.com/mattn/go-sqlite3 throws compilation error.

    gcc throws: internal compiler error

    Remove the download repository from your disk and try re-install with:

    go install github.com/mattn/go-sqlite3
    

User Authentication

This package supports the SQLite User Authentication module.

Compile

To use the User authentication module the package has to be compiled with the tag sqlite_userauth. See Features.

Usage

Create protected database

To create a database protected by user authentication provide the following argument to the connection string _auth. This will enable user authentication within the database. This option however requires two additional arguments:

  • _auth_user
  • _auth_pass

When _auth is present on the connection string user authentication will be enabled and the provided user will be created as an admin user. After initial creation, the parameter _auth has no effect anymore and can be omitted from the connection string.

Example connection string:

Create an user authentication database with user admin and password admin.

file:test.s3db?_auth&_auth_user=admin&_auth_pass=admin

Create an user authentication database with user admin and password admin and use SHA1 for the password encoding.

file:test.s3db?_auth&_auth_user=admin&_auth_pass=admin&_auth_crypt=sha1

Password Encoding

The passwords within the user authentication module of SQLite are encoded with the SQLite function sqlite_cryp. This function uses a ceasar-cypher which is quite insecure. This library provides several additional password encoders which can be configured through the connection string.

The password cypher can be configured with the key _auth_crypt. And if the configured password encoder also requires an salt this can be configured with _auth_salt.

Available Encoders

  • SHA1
  • SSHA1 (Salted SHA1)
  • SHA256
  • SSHA256 (salted SHA256)
  • SHA384
  • SSHA384 (salted SHA384)
  • SHA512
  • SSHA512 (salted SHA512)

Restrictions

Operations on the database regarding to user management can only be preformed by an administrator user.

Support

The user authentication supports two kinds of users

  • administrators
  • regular users

User Management

User management can be done by directly using the *SQLiteConn or by SQL.

SQL

The following sql functions are available for user management.

FunctionArgumentsDescription
authenticateusername string, password stringWill authenticate an user, this is done by the connection; and should not be used manually.
auth_user_addusername string, password string, admin intThis function will add an user to the database.
if the database is not protected by user authentication it will enable it. Argument admin is an integer identifying if the added user should be an administrator. Only Administrators can add administrators.
auth_user_changeusername string, password string, admin intFunction to modify an user. Users can change their own password, but only an administrator can change the administrator flag.
authUserDeleteusername stringDelete an user from the database. Can only be used by an administrator. The current logged in administrator cannot be deleted. This is to make sure their is always an administrator remaining.

These functions will return an integer.

  • 0 (SQLITE_OK)
  • 23 (SQLITE_AUTH) Failed to perform due to authentication or insufficient privileges
Examples
// Autheticate user
// Create Admin User
SELECT auth_user_add('admin2', 'admin2', 1);

// Change password for user
SELECT auth_user_change('user', 'userpassword', 0);

// Delete user
SELECT user_delete('user');

*SQLiteConn

The following functions are available for User authentication from the *SQLiteConn.

FunctionDescription
Authenticate(username, password string) errorAuthenticate user
AuthUserAdd(username, password string, admin bool) errorAdd user
AuthUserChange(username, password string, admin bool) errorModify user
AuthUserDelete(username string) errorDelete user

Attached database

When using attached databases. SQLite will use the authentication from the main database for the attached database(s).

Extensions

If you want your own extension to be listed here or you want to add a reference to an extension; please submit an Issue for this.

Spatialite

Spatialite is available as an extension to SQLite, and can be used in combination with this repository. For an example see shaxbee/go-spatialite.

FAQ

  • Getting insert error while query is opened.

    You can pass some arguments into the connection string, for example, a URI. See: #39

  • Do you want to cross compile? mingw on Linux or Mac?

    See: #106 See also: http://www.limitlessfx.com/cross-compile-golang-app-for-windows-from-linux.html

  • Want to get time.Time with current locale

    Use _loc=auto in SQLite3 filename schema like file:foo.db?_loc=auto.

  • Can I use this in multiple routines concurrently?

    Yes for readonly. But, No for writable. See #50, #51, #209, #274.

  • Why I'm getting no such table error?

    Why is it racy if I use a sql.Open("sqlite3", ":memory:") database?

    Each connection to :memory: opens a brand new in-memory sql database, so if the stdlib‘s sql engine happens to open another connection and you’ve only specified “:memory:”, that connection will see a brand new database. A workaround is to use “file::memory:?mode=memory&cache=shared”. Every connection to this string will point to the same in-memory database.

    For more information see

  • Reading from database with large amount of goroutines fails on OSX.

    OS X limits OS-wide to not have more than 1000 files open simultaneously by default.

    For more information see #289

  • Trying to execute a . (dot) command throws an error.

    Error: Error: near ".": syntax error Dot command are part of SQLite3 CLI not of this library.

    You need to implement the feature or call the sqlite3 cli.

    More infomation see #305

  • Error: database is locked

    When you get an database is locked. Please use the following options.

    Add to DSN: cache=shared

    Example:

    db, err := sql.Open("sqlite3", "file:locked.sqlite?cache=shared")
    

    Second please set the database connections of the SQL package to 1.

    db.SetMaxOpenConn(1)
    

    More information see #209

License

MIT: http://mattn.mit-license.org/2018

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.

Author

Yasuhiro Matsumoto (a.k.a mattn)

G.J.R. Timmer