sqlite3
Asynchronous, non-blocking SQLite3 bindings
Last updated 13 days ago .
· Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install sqlite3 
SYNC missed versions from official npm registry.

⚙️ node-sqlite3

Asynchronous, non-blocking SQLite3 bindings for Node.js.

Latest release Build Status FOSSA Status N-API v3 Badge N-API v6 Badge

Features

Installing

You can use npm or yarn to install sqlite3:

  • (recommended) Latest package:
npm install sqlite3
# or
yarn add sqlite3
  • GitHub's master branch: npm install https://github.com/tryghost/node-sqlite3/tarball/master

Prebuilt binaries

sqlite3 v5+ was rewritten to use Node-API so prebuilt binaries do not need to be built for specific Node versions. sqlite3 currently builds for both Node-API v3 and v6. Check the Node-API version matrix to ensure your Node version supports one of these. The prebuilt binaries should be supported on Node v10+.

The module uses node-pre-gyp to download the prebuilt binary for your platform, if it exists. These binaries are hosted on GitHub Releases for sqlite3 versions above 5.0.2, and they are hosted on S3 otherwise. The following targets are currently provided:

Format: napi-v{napi_build_version}-{platform}-{libc}-{arch}

  • napi-v3-darwin-unknown-x64
  • napi-v3-linux-glibc-arm64
  • napi-v3-linux-glibc-x64
  • napi-v3-linux-musl-arm64
  • napi-v3-linux-musl-x64
  • napi-v3-win32-unknown-ia32
  • napi-v3-win32-unknown-x64
  • napi-v6-darwin-unknown-x64
  • napi-v6-linux-glibc-arm64
  • napi-v6-linux-glibc-x64
  • napi-v6-linux-musl-arm64
  • napi-v6-linux-musl-x64
  • napi-v6-win32-unknown-ia32
  • napi-v6-win32-unknown-x64

Unfortunately, node-pre-gyp cannot differentiate between armv6 and armv7, and instead uses arm as the {arch}. Until that is fixed, you will still need to install sqlite3 from source.

Support for other platforms and architectures may be added in the future if CI supports building on them.

If your environment isn't supported, it'll use node-gyp to build SQLite but you will need to install a C++ compiler and linker.

Other ways to install

It is also possible to make your own build of sqlite3 from its source instead of its npm package (See below.).

The sqlite3 module also works with node-webkit if node-webkit contains a supported version of Node.js engine. (See below.)

SQLite's SQLCipher extension is also supported. (See below.)

API

See the API documentation in the wiki.

Usage

Note: the module must be installed before use.

const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database(':memory:');

db.serialize(() => {
    db.run("CREATE TABLE lorem (info TEXT)");

    const stmt = db.prepare("INSERT INTO lorem VALUES (?)");
    for (let i = 0; i < 10; i++) {
        stmt.run("Ipsum " + i);
    }
    stmt.finalize();

    db.each("SELECT rowid AS id, info FROM lorem", (err, row) => {
        console.log(row.id + ": " + row.info);
    });
});

db.close();

Source install

To skip searching for pre-compiled binaries, and force a build from source, use

npm install --build-from-source

The sqlite3 module depends only on libsqlite3. However, by default, an internal/bundled copy of sqlite will be built and statically linked, so an externally installed sqlite3 is not required.

If you wish to install against an external sqlite then you need to pass the --sqlite argument to npm wrapper:

npm install --build-from-source --sqlite=/usr/local

If building against an external sqlite3 make sure to have the development headers available. Mac OS X ships with these by default. If you don't have them installed, install the -dev package with your package manager, e.g. apt-get install libsqlite3-dev for Debian/Ubuntu. Make sure that you have at least libsqlite3 >= 3.6.

Note, if building against homebrew-installed sqlite on OS X you can do:

npm install --build-from-source --sqlite=/usr/local/opt/sqlite/

By default the node-gyp install will use python as part of the installation. A different python executable can be specified on the command line.

npm install --build-from-source --python=/usr/bin/python2

This uses the npm_config_python config, so values in .npmrc will be honoured:

python=/usr/bin/python2

Custom file header (magic)

The default sqlite file header is "SQLite format 3".
You can specify a different magic, though this will make standard tools and libraries unable to work with your files.

npm install --build-from-source --sqlite_magic="MyCustomMagic15"

Note that the magic must be exactly 15 characters long (16 bytes including null terminator).

Building for node-webkit

Because of ABI differences, sqlite3 must be built in a custom to be used with node-webkit.

To build node-sqlite3 for node-webkit:

  1. Install nw-gyp globally: npm install nw-gyp -g (unless already installed)

  2. Build the module with the custom flags of --runtime, --target_arch, and --target:

NODE_WEBKIT_VERSION="0.8.6" # see latest version at https://github.com/rogerwang/node-webkit#downloads
npm install sqlite3 --build-from-source --runtime=node-webkit --target_arch=ia32 --target=$(NODE_WEBKIT_VERSION)

This command internally calls out to node-pre-gyp which itself calls out to nw-gyp when the --runtime=node-webkit option is passed.

You can also run this command from within a node-sqlite3 checkout:

npm install --build-from-source --runtime=node-webkit --target_arch=ia32 --target=$(NODE_WEBKIT_VERSION)

Remember the following:

  • You must provide the right --target_arch flag. ia32 is needed to target 32bit node-webkit builds, while x64 will target 64bit node-webkit builds (if available for your platform).

  • After the sqlite3 package is built for node-webkit it cannot run in the vanilla Node.js (and vice versa).

    • For example, npm test of the node-webkit's package would fail.

Visit the “Using Node modules” article in the node-webkit's wiki for more details.

Building for SQLCipher

For instructions for building sqlcipher see Building SQLCipher for node.js

To run node-sqlite3 against sqlcipher you need to compile from source by passing build options like:

npm install sqlite3 --build-from-source --sqlite_libname=sqlcipher --sqlite=/usr/

node -e 'require("sqlite3")'

If your sqlcipher is installed in a custom location (if you compiled and installed it yourself), you'll also need to to set some environment variables:

On OS X with Homebrew

Set the location where brew installed it:

export LDFLAGS="-L`brew --prefix`/opt/sqlcipher/lib"
export CPPFLAGS="-I`brew --prefix`/opt/sqlcipher/include"
npm install sqlite3 --build-from-source --sqlite_libname=sqlcipher --sqlite=`brew --prefix`

node -e 'require("sqlite3")'

On most Linuxes (including Raspberry Pi)

Set the location where make installed it:

export LDFLAGS="-L/usr/local/lib"
export CPPFLAGS="-I/usr/local/include -I/usr/local/include/sqlcipher"
export CXXFLAGS="$CPPFLAGS"
npm install sqlite3 --build-from-source --sqlite_libname=sqlcipher --sqlite=/usr/local --verbose

node -e 'require("sqlite3")'

Custom builds and Electron

Running sqlite3 through electron-rebuild does not preserve the sqlcipher extension, so some additional flags are needed to make this build Electron compatible. Your npm install sqlite3 --build-from-source command needs these additional flags (be sure to replace the target version with the current Electron version you are working with):

--runtime=electron --target=1.7.6 --dist-url=https://electronjs.org/headers

In the case of MacOS with Homebrew, the command should look like the following:

npm install sqlite3 --build-from-source --sqlite_libname=sqlcipher --sqlite=`brew --prefix` --runtime=electron --target=1.7.6 --dist-url=https://electronjs.org/headers

Testing

npm test

Contributors

Acknowledgments

Thanks to Orlando Vazquez, Eric Fredricksen and Ryan Dahl for their SQLite bindings for node, and to mraleph on Freenode's #v8 for answering questions.

This module was originally created by Mapbox & is now maintained by Ghost.

License

node-sqlite3 is BSD licensed.

FOSSA Status

Current Tags

  • 3.1.10-alpha                                ...           alpha (5 years ago)
  • 5.0.8                                ...           latest (13 days ago)

91 Versions

  • 5.0.2                                ...           a year ago
  • 5.0.1                                ...           a year ago
  • 5.0.0                                ...           2 years ago
  • 4.2.0                                ...           2 years ago
  • 4.1.1                                ...           2 years ago
  • 4.1.0                                ...           3 years ago
  • 4.0.9                                ...           3 years ago
  • 4.0.8                                ...           3 years ago
  • 4.0.7                                ...           3 years ago
  • 4.0.6                                ...           3 years ago
  • 4.0.4                                ...           4 years ago
  • 4.0.3                                ...           4 years ago
  • 4.0.2                                ...           4 years ago
  • 4.0.1                                ...           4 years ago
  • 4.0.0                                ...           4 years ago
  • 3.1.13                                ...           5 years ago
  • 3.1.12                                ...           5 years ago
  • 3.1.11                                ...           5 years ago
  • 3.1.10                                ...           5 years ago
  • 3.1.10-alpha [deprecated]           ...           5 years ago
  • 3.1.9                                ...           5 years ago
  • 3.1.8                                ...           6 years ago
  • 3.1.7                                ...           6 years ago
  • 3.1.6                                ...           6 years ago
  • 3.1.5                                ...           6 years ago
  • 3.1.4                                ...           6 years ago
  • 3.1.3                                ...           6 years ago
  • 3.1.2                                ...           6 years ago
  • 3.1.1                                ...           7 years ago
  • 3.1.0                                ...           7 years ago
  • 3.0.10                                ...           7 years ago
  • 3.0.9                                ...           7 years ago
  • 3.0.8                                ...           7 years ago
  • 3.0.7                                ...           7 years ago
  • 3.0.6                                ...           7 years ago
  • 3.0.5                                ...           7 years ago
  • 3.0.4                                ...           8 years ago
  • 3.0.2                                ...           8 years ago
  • 3.0.1                                ...           8 years ago
  • 3.0.0                                ...           8 years ago
  • 2.2.7                                ...           8 years ago
  • 2.2.6                                ...           8 years ago
  • 2.2.4                                ...           8 years ago
  • 2.2.3                                ...           8 years ago
  • 2.2.2                                ...           8 years ago
  • 2.2.1                                ...           8 years ago
  • 2.2.0                                ...           8 years ago
  • 2.1.19                                ...           9 years ago
  • 2.1.18                                ...           9 years ago
  • 2.1.17                                ...           9 years ago
  • 2.1.16                                ...           9 years ago
  • 2.1.15                                ...           9 years ago
  • 2.1.14                                ...           9 years ago
  • 2.1.13                                ...           9 years ago
  • 2.1.12                                ...           9 years ago
  • 2.1.11                                ...           9 years ago
  • 2.1.10                                ...           9 years ago
  • 2.1.9                                ...           9 years ago
  • 2.1.8                                ...           9 years ago
  • 2.1.7                                ...           9 years ago
  • 2.1.5                                ...           10 years ago
  • 2.1.4                                ...           10 years ago
  • 2.1.3                                ...           10 years ago
  • 2.1.2                                ...           10 years ago
  • 2.1.1                                ...           11 years ago
  • 2.1.0                                ...           11 years ago
  • 2.0.18                                ...           11 years ago
  • 2.0.17                                ...           11 years ago
  • 2.0.16                                ...           11 years ago
  • 2.0.15                                ...           11 years ago
  • 2.0.14                                ...           11 years ago
  • 2.0.13                                ...           11 years ago
  • 2.0.12                                ...           11 years ago
  • 2.0.11                                ...           11 years ago
  • 2.0.10                                ...           11 years ago
  • 2.0.9                                ...           11 years ago
  • 2.0.8                                ...           11 years ago
  • 2.0.7                                ...           11 years ago
  • 2.0.6                                ...           11 years ago
  • 2.0.5                                ...           11 years ago
  • 2.0.4                                ...           11 years ago
  • 2.0.3                                ...           11 years ago
  • 2.0.2                                ...           11 years ago
  • 2.0.1                                ...           11 years ago
  • 2.0.0                                ...           11 years ago
  • 5.0.3                                ...           a month ago
  • 5.0.4                                ...           a month ago
  • 5.0.5                                ...           a month ago
  • 5.0.6                                ...           22 days ago
  • 5.0.7                                ...           14 days ago
  • 5.0.8                                ...           13 days ago

Copyright © npmmirror.com | 浙ICP备15033595号-63 |