sqlite3 driver for go using database/sql

Overview

go-sqlite3

GoDoc Reference GitHub Actions Financial Contributors on Open Collective codecov Go Report Card

Latest stable version is v1.14 or later not v2.

NOTE: The increase to v2 was an accident. There were no major changes or features.

Description

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

Supported Golang version: See .github/workflows/go.yaml

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
Name Key Value(s) Description
UA - Create _auth - Create User Authentication, for more information see User Authentication
UA - Username _auth_user string Username for User Authentication, for more information see User Authentication
UA - Password _auth_pass string Password for User Authentication, for more information see User Authentication
UA - Crypt _auth_crypt
  • SHA1
  • SSHA1
  • SHA256
  • SSHA256
  • SHA384
  • SSHA384
  • SHA512
  • SSHA512
Password encoder to use for User Authentication, for more information see User Authentication
UA - Salt _auth_salt string Salt to use if the configure password encoder requires a salt, for User Authentication, for more information see User Authentication
Auto Vacuum _auto_vacuum | _vacuum
  • 0 | none
  • 1 | full
  • 2 | incremental
For more information see PRAGMA auto_vacuum
Busy Timeout _busy_timeout | _timeout int Specify value for sqlite3_busy_timeout. For more information see PRAGMA busy_timeout
Case Sensitive LIKE _case_sensitive_like | _cslike boolean For more information see PRAGMA case_sensitive_like
Defer Foreign Keys _defer_foreign_keys | _defer_fk boolean For more information see PRAGMA defer_foreign_keys
Foreign Keys _foreign_keys | _fk boolean For more information see PRAGMA foreign_keys
Ignore CHECK Constraints _ignore_check_constraints boolean For more information see PRAGMA ignore_check_constraints
Immutable immutable boolean For more information see Immutable
Journal Mode _journal_mode | _journal
  • DELETE
  • TRUNCATE
  • PERSIST
  • MEMORY
  • WAL
  • OFF
For more information see PRAGMA journal_mode
Locking Mode _locking_mode | _locking
  • NORMAL
  • EXCLUSIVE
For more information see PRAGMA locking_mode
Mode mode
  • ro
  • rw
  • rwc
  • memory
Access Mode of the database. For more information see SQLite Open
Mutex Locking _mutex
  • no
  • full
Specify mutex mode.
Query Only _query_only boolean For more information see PRAGMA query_only
Recursive Triggers _recursive_triggers | _rt boolean For more information see PRAGMA recursive_triggers
Secure Delete _secure_delete boolean | FAST For more information see PRAGMA secure_delete
Shared-Cache Mode cache
  • shared
  • private
Set cache mode for more information see sqlite.org
Synchronous _synchronous | _sync
  • 0 | OFF
  • 1 | NORMAL
  • 2 | FULL
  • 3 | EXTRA
For more information see PRAGMA synchronous
Time Zone Location _loc auto Specify location of time format.
Transaction Lock _txlock
  • immediate
  • deferred
  • exclusive
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

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

Extension Build Tag Description
Additional Statistics sqlite_stat4 This 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 Authority sqlite_allow_uri_authority URI 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 Armor sqlite_app_armor When 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 Extensions sqlite_omit_load_extension Loading of external extensions is enabled by default.

To disable extension loading add the build tag sqlite_omit_load_extension.
Foreign Keys sqlite_foreign_keys This 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 Vacuum sqlite_vacuum_full Set the default auto vacuum to full
Incremental Auto Vacuum sqlite_vacuum_incr Set the default auto vacuum to incremental
Full Text Search Engine sqlite_fts5 When 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 Unicode sqlite_icu This option causes the International Components for Unicode or "ICU" extension to SQLite to be added to the build
Introspect PRAGMAS sqlite_introspect This option adds some extra PRAGMA statements.
  • PRAGMA function_list
  • PRAGMA module_list
  • PRAGMA pragma_list
JSON SQL Functions sqlite_json When this option is defined in the amalgamation, the JSON SQL functions are added to the build automatically
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.

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_fast For more information see PRAGMA secure_delete
Tracing / Debug sqlite_trace Activate trace functions
User Authentication sqlite_userauth SQLite 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.

Cross Compiling from MAC OSX

The simplest way to cross compile from OSX is to use xgo.

Steps:

  • Install xgo (go get github.com/karalabe/xgo).
  • Ensure that your project is within your GOPATH.
  • Run xgo local/path/to/project.

Please refer to the project's 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.

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

Function Arguments Description
authenticate username string, password string Will authenticate an user, this is done by the connection; and should not be used manually.
auth_user_add username string, password string, admin int This 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_change username string, password string, admin int Function to modify an user. Users can change their own password, but only an administrator can change the administrator flag.
authUserDelete username string Delete 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.

Function Description
Authenticate(username, password string) error Authenticate user
AuthUserAdd(username, password string, admin bool) error Add user
AuthUserChange(username, password string, admin bool) error Modify user
AuthUserDelete(username string) error Delete 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.

extension-functions.c from SQLite3 Contrib

extension-functions.c is available as an extension to SQLite, and provides the following functions:

  • Math: acos, asin, atan, atn2, atan2, acosh, asinh, atanh, difference, degrees, radians, cos, sin, tan, cot, cosh, sinh, tanh, coth, exp, log, log10, power, sign, sqrt, square, ceil, floor, pi.
  • String: replicate, charindex, leftstr, rightstr, ltrim, rtrim, trim, replace, reverse, proper, padl, padr, padc, strfilter.
  • Aggregate: stdev, variance, mode, median, lower_quartile, upper_quartile

For an example see dinedal/go-sqlite3-extension-functions.

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:?cache=shared" (or "file:foobar?mode=memory&cache=shared"). Every connection to this string will point to the same in-memory database.

    Note that if the last database connection in the pool closes, the in-memory database is deleted. Make sure the max idle connection limit is > 0, and the connection lifetime is infinite.

    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 information see #305

  • Error: database is locked

    When you get a 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.SetMaxOpenConns(1)

    More information see #209

Contributors

Code Contributors

This project exists thanks to all the people who contribute. [Contribute].

Financial Contributors

Become a financial contributor and help us sustain our community. [Contribute]

Individuals

Organizations

Support this project with your organization. Your logo will show up here with a link to your website. [Contribute]

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

Comments
  • Works with 64-bit Windows (without pkg-config) but doesn't work with 32-bit Windows.

    Works with 64-bit Windows (without pkg-config) but doesn't work with 32-bit Windows.

    Hello. I've been having trouble with getting this to work on Windows using 32-bit and then found that it works on 64-bit Windows even without pkg-config (which incidentally I had got installed though I'm not sure that could help after all). Having learned more about the current head revision I understand that it works without pkg-config on Windows however I could not get it to work with 32-bit Windows and I get a stream of error messages like "__divdi3: not defined". My understanding of C and cgo is patchy and I'm not sure what this means. I couldn't see why it should be different on 32-bit Windows but perhaps some other compiler option is required.

    I resolved my problems by discarding pkg-config and the static compilation on Windows and use the DLL instead, which is what other platforms are using anyway. So instead of the #cgo pkg-config line I'm using "#cgo LDFLAGS: -lsqlite3" having the environment set up with the include files on the path for gcc (LIBRARY_PATH and C_INCLUDE_PATH). The little work with cgo that I've done so far this works best with the smallest of changes to the source-code across platforms.

    I've never used pkg-config before and while I had no trouble with it on other platforms on Windows it was a struggle getting an environment set up. Wouldn't this be better without pkg-config?

    In any case the current head revision doesn't appear to work for 32-bit Windows and the static compilation as an exception for Windows seems unnecessarily complicated. Maybe you can get it to work but I wonder if this is the wrong direction to go.

    opened by stever 85
  • Cross platform build failed

    Cross platform build failed

    When I tried to build from OS X for Linux, it says:

    $ GOOS=linux GOARCH=amd64 go install
    # github.com/mattn/go-sqlite3
    ../../../mattn/go-sqlite3/sqlite3.c:241 unknown #: if
    ../../../mattn/go-sqlite3/sqlite3.c:256 unknown #: if
    ../../../mattn/go-sqlite3/sqlite3.c:296 unknown #: if
    ../../../mattn/go-sqlite3/sqlite3.c:309 unknown #: if
    ../../../mattn/go-sqlite3/sqlite3.c:339 unknown #: if
    ../../../mattn/go-sqlite3/sqlite3.c:342 unknown #: elif
    ../../../mattn/go-sqlite3/sqlite3.c:343 macro redefined: SQLITE_INT_TO_PTR
    ../../../mattn/go-sqlite3/sqlite3.c:344 macro redefined: SQLITE_PTR_TO_INT
    ../../../mattn/go-sqlite3/sqlite3.c:345 unknown #: elif
    ../../../mattn/go-sqlite3/sqlite3.c:346 macro redefined: SQLITE_INT_TO_PTR
    ../../../mattn/go-sqlite3/sqlite3.c:347 macro redefined: SQLITE_PTR_TO_INT
    too many errors
    
    opened by elgs 68
  • Install without gcc (not possible)

    Install without gcc (not possible)

    Is it possible to use the library in Go without recompiling the binaries?https://github.com/lxc/lxd/issues/762#issuecomment-112463214

    I mean is it possible to compile binary pieces of SQLite needed be go-sqlite3 and ship them in separate repository, so that only Go itself can be used to assemble the final product together? Maybe Go already provides some helpers/tools to do that?

    opened by techtonik 52
  • Cross compiling fails

    Cross compiling fails

    Very possibly a mistake on my part, if so please let me know where I went wrong. My first time with Go today!

    I have a basic Go file using this library. I am on macOS 10.12.3, Go 1.8 darwin/amd64.

    Succeeds: env GOOS=darwin go build -i app.go

    Fails: env GOOS=linux go build -i app.go

    Specifying GOARCH=amd64 makes no difference.

    Errors:

    ../../../go/src/github.com/mattn/go-sqlite3/sqlite3_go18.go:18: undefined: SQLiteConn
    ../../../go/src/github.com/mattn/go-sqlite3/sqlite3_go18.go:26: undefined: SQLiteConn
    ../../../go/src/github.com/mattn/go-sqlite3/sqlite3_go18.go:27: undefined: namedValue
    ../../../go/src/github.com/mattn/go-sqlite3/sqlite3_go18.go:29: undefined: namedValue
    ../../../go/src/github.com/mattn/go-sqlite3/sqlite3_go18.go:35: undefined: SQLiteConn
    ../../../go/src/github.com/mattn/go-sqlite3/sqlite3_go18.go:36: undefined: namedValue
    ../../../go/src/github.com/mattn/go-sqlite3/sqlite3_go18.go:44: undefined: SQLiteConn
    ../../../go/src/github.com/mattn/go-sqlite3/sqlite3_go18.go:49: undefined: SQLiteConn
    ../../../go/src/github.com/mattn/go-sqlite3/sqlite3_go18.go:54: undefined: SQLiteStmt
    ../../../go/src/github.com/mattn/go-sqlite3/sqlite3_go18.go:63: undefined: SQLiteStmt
    ../../../go/src/github.com/mattn/go-sqlite3/sqlite3_go18.go:36: too many errors
    

    Said very basic Go file causing this, in-case it helps:

    package main
    
    import (
    	"database/sql"
    	"log"
    	"os"
    	_ "github.com/mattn/go-sqlite3"
    )
    
    // Bookmark represents a single row from database
    type Bookmark struct {
    	Id       int
    	Url      string
    	Metadata string
    	Tags     string
    	Desc     string
    	Flags    int
    }
    
    func main() {
    	dbpath := GetDbPath()
    
    	db := InitDB(dbpath)
    	defer db.Close()
    
    	bookmarks := GetAllBookmarks(db)
    	log.Print(bookmarks)
    }
    
    // Write errors to log & quit
    func CheckError(err error) {
    	if err != nil {
    		log.Fatal(err)
    	}
    }
    
    func InitDB(filepath string) *sql.DB {
    	db, err := sql.Open("sqlite3", filepath)
    	CheckError(err)
    	if db == nil {
    		panic("db nil")
    	}
    	return db
    }
    
    func GetAllBookmarks(db *sql.DB) []Bookmark {
    	const queryAll string = "SELECT * FROM bookmarks;"
    
    	rows, err := db.Query(queryAll)
    	CheckError(err)
    	defer rows.Close()
    
    	var result []Bookmark
    	for rows.Next() {
    		item := Bookmark{}
    		err2 := rows.Scan(&item.Id, &item.Url, &item.Metadata, &item.Tags, &item.Desc, &item.Flags)
    		CheckError(err2)
    		result = append(result, item)
    	}
    
    	return result
    }
    
    func GetDbPath() string {
    	const dbFileName string = "bookmarks.db"
    
    	var dir = os.Getenv("XDG_DATA_HOME")
    	if dir != "" {
    		dir += "/buku/"
    	} else {
    		dir = os.Getenv("HOME") + "/.local/share/buku/"
    	}
    
    	dir += dbFileName
    
    	return dir
    }
    
    opened by samhh 40
  • Issue installing on Mac OS

    Issue installing on Mac OS

    Had some problems installing the lib on Mac, so just wanted to leave this here for future people searching for help:

    after

    brew install pkgconfig
    brew install sqlite3
    

    I've tried pkg-config --cflags --libs sqlite3, but got:

    # pkg-config --cflags sqlite3
    Package sqlite3 was not found in the pkg-config search path.
    Perhaps you should add the directory containing `sqlite3.pc'
    to the PKG_CONFIG_PATH environment variable
    No package 'sqlite3' found
    exit status 1
    

    What you need to do:

    sudo PKG_CONFIG_PATH=/usr/local/Cellar/sqlite/3.7.15.2/lib/pkgconfig/ go get github.com/mattn/go-sqlite3
    

    Please replace 3.7.15.2 with your version.

    Thanks for the lib!

    opened by slava-vishnyakov 39
  • Problem with unstable  go-sqlite3 version - 2.0.3

    Problem with unstable go-sqlite3 version - 2.0.3

    Hi,

    We at the platform team of razorpay internally use bulk-insert which internally uses github.com/jinzhu/gorm v1.9.12 and creates an indirect dependency on github.com/mattn/go-sqlite3 v2.0.3.

    Reference :- https://github.com/t-tiger/gorm-bulk-insert/blob/master/go.mod#L14

    Now, this setup was working fine for us till last week and suddenly we had a failure in our docker build for github actions citing github.com/mattn/[email protected]+incompatible: unknown revision v2.0.3

    We also saw one of your issues where you mentioned versions 2.0+ were unstable - issue

    Did you stop supporting 2.0.3 version suddenly in the last 7-10 days. We were wondering what caused the breakage of the build from our side. It would be helpful if you could throw some light on this issue.

    Thanks, Shriram

    opened by Shriram-RP 37
  • upstream 3.21.0 with UPDATE/DELETE LIMIT support

    upstream 3.21.0 with UPDATE/DELETE LIMIT support

    Amalgamation built with

    CFLAGS='-std=gnu99 -DSQLITE_ENABLE_RTREE -DSQLITE_THREADSAFE=1 -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_FTS4_UNICODE61 -DSQLITE_TRACE_SIZE_LIMIT=15 -DSQLITE_DISABLE_INTRINSIC -Wno-deprecated-declarations' ./configure --enable-rtree --enable-fts3 --enable-fts4 --enable-json1 --enable-update-limit
    

    maybe defining flags before configure was too much, sorry for mess.

    Originally proposed in https://github.com/mattn/go-sqlite3/issues/213 https://github.com/mattn/go-sqlite3/pull/215

    opened by temoto 36
  • SELECT Query not working

    SELECT Query not working

    I have SQLite3 3.15.x on Windows 7 64 bit but the GCC and go (version 1.1) are 32 bit. The SELECT queries are not working for some reason. At times its leads to PANIC when the query is executed. Is there an issue with 32 bit version on Windows ? The following is the error returned with err.Error() : U??S??►??3?

    opened by raheelgup 31
  • cannot load github.com/mattn/go-sqlite3: module github.com/mattn/go-sqlite3@latest (v2.0.0+incompatible) found, but does not contain package github.com/mattn/go-sqlite3

    cannot load github.com/mattn/go-sqlite3: module github.com/mattn/[email protected] (v2.0.0+incompatible) found, but does not contain package github.com/mattn/go-sqlite3

    This package doesn't seem to work with go modules. Here's a simple recipe in Docker, to make it easy to reproduce. Please tell me what I'm doing wrong:

    $ docker run -it --rm golang:alpine
    /go # apk add build-base
    ...
    /go # mkdir /src
    /go # cd /src
    /src # cat - > main.go
    package main
    import (
            "fmt"
    	_ "github.com/mattn/go-sqlite3"
    )
    func main() {
    	fmt.Println("Hello world!")
    }
    /src # export CGO_ENABLED=1
    /src # go mod init example.com
    go: creating new go.mod: module example.com
    /src # go get github.com/mattn/go-sqlite3
    go: finding github.com/mattn/go-sqlite3 v2.0.0+incompatible
    go: downloading github.com/mattn/go-sqlite3 v2.0.0+incompatible
    go: extracting github.com/mattn/go-sqlite3 v2.0.0+incompatible
    /src # go run .
    build example.com: cannot load github.com/mattn/go-sqlite3: module github.com/mattn/[email protected] (v2.0.0+incompatible) found, but does not contain package github.com/mattn/go-sqlite3
    
    opened by cjyar 29
  • database is locked

    database is locked

    We have a little app which has multiple goroutines all running queries and it often fails with "database is locked" :(

    we're running with:

    db, err := sql.Open("sqlite3", "test.db?cache=shared&mode=rwc")
    
    PRAGMA journal_mode = WAL;
    PRAGMA synchronous = NORMAL;
    

    sqlite3 -version 3.10.2 2016-01-20 15:27:19 17efb4209f97fb4971656086b138599a91a75ff9

    Is this a known issue?

    opened by stevenh 28
  • gcc issue on windows 64

    gcc issue on windows 64

    While trying to do "go install github.com/mattn/go-sqlite3" I am getting the follwoing error. In one of the issues someone mentioned that we don't need gcc if we install go-sqlite3. Any help would be greatly appreciated.

    github.com/mattn/go-sqlite3

    exec: "gcc": executable file not found in %PATH%

    Thanks Arul

    opened by arulrajalivi 27
  • create a index on not exist column will lead user CAN NOT query database by command line

    create a index on not exist column will lead user CAN NOT query database by command line

    hi, there may be 2 bugs about sqlite3.

    1. when i execute a sql by go api to create a index on not exist column , it will lead me can not query databse by command line.
    2. i can create a index on a column by command line ,even if this column does not exist in table.
    opened by rencalo770 1
  • cross compiling from linux to mac failed

    cross compiling from linux to mac failed

    When I cross compiled a program that used the [SQLite](https://github.com/mattn/go-sqlite3) package and cross compiled from linux to mac, the build failed.

    go env

    GO111MODULE=""
    GOARCH="amd64"
    GOBIN=""
    GOCACHE="/root/.cache/go-build"
    GOENV="/root/.config/go/env"
    GOEXE=""
    GOEXPERIMENT=""
    GOFLAGS=""
    GOHOSTARCH="amd64"
    GOHOSTOS="linux"
    GOINSECURE=""
    GOMODCACHE="/home/go/gopath/pkg/mod"
    GONOPROXY=""
    GONOSUMDB=""
    GOOS="linux"
    GOPATH="/home/go/gopath"
    GOPRIVATE=""
    GOPROXY="https://goproxy.cn,direct"
    GOROOT="/home/go"
    GOSUMDB="sum.golang.org"
    GOTMPDIR=""
    GOTOOLDIR="/home/go/pkg/tool/linux_amd64"
    GOVCS=""
    GOVERSION="go1.17.8"
    GCCGO="gccgo"
    AR="ar"
    CC="gcc"
    CXX="g++"
    CGO_ENABLED="1"
    GOMOD="/tmp/test//go.mod"
    CGO_CFLAGS="-g -O2"
    CGO_CPPFLAGS=""
    CGO_CXXFLAGS="-g -O2"
    CGO_FFLAGS="-g -O2"
    CGO_LDFLAGS="-g -O2"
    PKG_CONFIG="pkg-config"
    

    go version

    go version go1.17.8 linux/amd64
    

    linux version

    Linux 3.10.0-1160.71.1.el7.x86_64
    

    error message:

    [[email protected] test]# CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 go build -o darwin_amd6 *.go
    # runtime/cgo
    gcc: error: x86_64: No such file or directory
    gcc: error: unrecognized command line option '-arch'
    

    I know maybe I need to specify a C toolchain when cross compiling CGO, but I haven't found one for macos(amd64) and m1 yet.

    Is there anyone who can help?😊

    opened by 2hangchen 2
  • error when running on SCRATCH image ( partially solved )

    error when running on SCRATCH image ( partially solved )

    Hello everyone, I tried to build a module using your go-sqlite3 package and run the resulting binary inside a SCRATCH image.

    The build is done inside a github ubuntu runner, with basic go-install action + apt-install build-essential.

    Unfortunately when running the container, I got a runtime error which was exec /go/src/{my_binary_name}: no such file or directory

    After various search found out it was due to dynamic linking, managed to make everything works by adding those flags to the goreleaser

        ldflags: -s -w -linkmode external -extldflags "-static"
        flags: --tags netgo
    

    I am not good enough to understand what happens under the hood, and why it solved it, but maybe it could be usefull to some people to improve the documentation and this use case.

    Ps: can provide more details in order to reproduce it, just let me know.

    Regards, JC

    opened by jCOTINEAU 0
  • Increment of in-memory database size

    Increment of in-memory database size

    Is there a way to utilize configurations like in https://www.sqlite.org/c3ref/c_config_covering_index_scan.html ? I want to know if I can run SQLite on going while setting SQLITE_MEMDB_DEFAULT_MAXSIZE.

    Alternatively I would like to be able to run sqlite3_config() as I understand this needs to be done before db init.

    Thank you.

    opened by Gilwe 0
  • Add Blob I/O

    Add Blob I/O

    Incremental Blob I/O

    This PR adds support for Incremental Blob I/O.

    Notes

    • Modeled after backup using a driver connection.
    • The interfaces io.Reader and io.Closer have been implemented.
    • Related to issue #239

    If this PR makes sense, I'll enhance it with additional io interfaces.

    opened by joriszwart 6
Releases(v1.10.0)
  • v1.10.0(May 29, 2019)

    Core

    • SQLite 3.28.0

    Updates

    • Fix type of variadic function
    • column types text, varchar, *char return as strings
    • Use SQLITE_OPEN_CREATE for sqlite3_open_v2
    • Fix data race in AutoCommit()
    Source code(tar.gz)
    Source code(zip)
  • v1.9.0(Jun 13, 2018)

    Core

    • SQLite 3.24.0

    Updates

    • Add OpenBSD
    • Increase coverage
    • Added Tests for sqlite_crypt encoders

    Repository Updates

    • Shorten build time on Travis-CI
    Source code(tar.gz)
    Source code(zip)
  • v1.8.0(Jun 12, 2018)

    Core

    • SQLite: 3.24.0

    New Features

    • Implemented SQLite User Authentication Module
    • Implemented GetFilename sqlite3_db_filename
    • Implemented use of PRAGMA's from DSN
    • Implemented usleep for Windows
    • PRAGMA DSN
      • Auto Vacuum
      • Case Sensitive LIKE
      • Defer Foreign Keys
      • Ignore CHECK Constraints
      • Journal Mode
      • Query Only
      • Secure Delete
      • Synchronous
      • User Authentication
      • Writeable Schema

    Updates

    • Rewrite upgrade tool to auto include UserAuth Module
    • FIX: Case-Sensitive values for PRAGMA
    • Reduced code for:
      • foreign_keys
      • recursive_triggers

    Fixes

    • #542
    • #568
    • ICU extension for darwin
    • Fixes WAL Mode Synchronous
    • #120
    • Compile Section Closes #175
    • Compile Section Closes #201
    • Compile Section Closes #206
    • Compile Section Closes #404
    • Compile Section Closes #217
    • Compile Section Closes #224
    • Compile Section Closes #234
    • Compile Section Closes #242
    • Feature table Closes #255
    • Description Section Closes #232
    • Golang:1.6 not supported Closes #272
    • Golang:1.5 not supported + compilation section Closes #283
    • usleep Implemented Closes #285
    • FAQ Section Closes #289
    • Compile Section closes #295
    • FAQ Section Closes #305
    • PR339 Closes #318 #321
    • Compilation Section Closes #341
    • PR407 Closes #364
    • Feature sqlite_vtable Closes #393
    • Compile Section Closes #416
    • sqlite_trace feature Closes #433
    • Compilation Section Closes #435
    • Compilation Section Closes #443
    • Golang:1.6 Not Supported Closes #445
    • Compilation Section Closes #451
    • Compilation Section Closes #467
    • Compilation Section Closes #491
    • Compilation Section Closes #495
    • Compilation Section Closes #505
    • Compilation Section Closes #557
    • Compilation Section Closes #560
    Source code(tar.gz)
    Source code(zip)
Owner
mattn
Long-time Golang user&contributor, Google Dev Expert for Go, and author of many Go tools, Vim plugin author. Windows hacker C#/Java/C/C++
mattn
Microsoft ActiveX Object DataBase driver for go that using exp/sql

go-adodb Microsoft ADODB driver conforming to the built-in database/sql interface Installation This package can be installed with the go get command:

mattn 129 Sep 14, 2022
Oracle driver for Go using database/sql

go-oci8 Description Golang Oracle database driver conforming to the Go database/sql interface Installation Install Oracle full client or Instant Clien

mattn 593 Sep 20, 2022
Go driver for PostgreSQL over SSH. This driver can connect to postgres on a server via SSH using the local ssh-agent, password, or private-key.

pqssh Go driver for PostgreSQL over SSH. This driver can connect to postgres on a server via SSH using the local ssh-agent, password, or private-key.

mattn 49 Sep 19, 2022
Go Sql Server database driver.

gofreetds Go FreeTDS wrapper. Native Sql Server database driver. Features: can be used as database/sql driver handles calling stored procedures handle

minus5 106 Jan 23, 2022
Pure Go Postgres driver for database/sql

pq - A pure Go postgres driver for Go's database/sql package Install go get github.com/lib/pq Features SSL Handles bad connections for database/sql S

null 7.6k Sep 26, 2022
Attach hooks to any database/sql driver

sqlhooks Attach hooks to any database/sql driver. The purpose of sqlhooks is to provide a way to instrument your sql statements, making really easy to

Gustavo Chaín 582 Sep 26, 2022
Qmgo - The Go driver for MongoDB. It‘s based on official mongo-go-driver but easier to use like Mgo.

Qmgo English | 简体中文 Qmgo is a Go driver for MongoDB . It is based on MongoDB official driver, but easier to use like mgo (such as the chain call). Qmg

Qiniu Cloud 970 Sep 21, 2022
Mirror of Apache Calcite - Avatica Go SQL Driver

Apache Avatica/Phoenix SQL Driver Apache Calcite's Avatica Go is a Go database/sql driver for the Avatica server. Avatica is a sub-project of Apache C

The Apache Software Foundation 102 Sep 17, 2022
Firebird RDBMS sql driver for Go (golang)

firebirdsql (Go firebird sql driver) Firebird RDBMS http://firebirdsql.org SQL driver for Go Requirements Firebird 2.5 or higher Golang 1.13 or higher

Hajime Nakagami 175 Sep 8, 2022
Microsoft SQL server driver written in go language

A pure Go MSSQL driver for Go's database/sql package Install Requires Go 1.8 or above. Install with go get github.com/denisenkom/go-mssqldb . Connecti

null 1.6k Sep 26, 2022
GO DRiver for ORacle DB

Go DRiver for ORacle godror is a package which is a database/sql/driver.Driver for connecting to Oracle DB, using Anthony Tuininga's excellent OCI wra

null 390 Sep 15, 2022
PostgreSQL driver and toolkit for Go

pgx - PostgreSQL Driver and Toolkit pgx is a pure Go driver and toolkit for PostgreSQL. pgx aims to be low-level, fast, and performant, while also ena

Jack Christensen 6k Sep 22, 2022
Lightweight Golang driver for ArangoDB

Arangolite Arangolite is a lightweight ArangoDB driver for Go. It focuses on pure AQL querying. See AranGO for a more ORM-like experience. IMPORTANT:

Fabien Herfray 72 Jun 17, 2022
Go language driver for RethinkDB

RethinkDB-go - RethinkDB Driver for Go Go driver for RethinkDB Current version: v6.2.1 (RethinkDB v2.4) Please note that this version of the driver on

RethinkDB 1.6k Sep 2, 2022
goriak - Go language driver for Riak KV

goriak Current version: v3.2.1. Riak KV version: 2.0 or higher, the latest version of Riak KV is always recommended. What is goriak? goriak is a wrapp

Gustav Westling 27 Jan 23, 2022
Mongo Go Models (mgm) is a fast and simple MongoDB ODM for Go (based on official Mongo Go Driver)

Mongo Go Models Important Note: We changed package name from github.com/Kamva/mgm/v3(uppercase Kamva) to github.com/kamva/mgm/v3(lowercase kamva) in v

kamva 564 Sep 23, 2022
The MongoDB driver for Go

The MongoDB driver for Go This fork has had a few improvements by ourselves as well as several PR's merged from the original mgo repo that are current

GlobalSign 2k Sep 28, 2022
The Go driver for MongoDB

MongoDB Go Driver The MongoDB supported driver for Go. Requirements Installation Usage Bugs / Feature Reporting Testing / Development Continuous Integ

mongodb 6.9k Sep 26, 2022
RethinkDB-go - RethinkDB Driver for Go

Go language driver for RethinkDB

RethinkDB 1.6k Sep 26, 2022