@131/sqlite3 中文文档教程
Supported platforms
sqlite3< /code> 模块适用于 Node.js v4.x、v6.x、v8.x、v10.x、v11.x 和 v12.x。
大多数 Node 版本和平台的二进制文件默认通过 node-pre-gyp 提供。
sqlite3
模块也适用于 node-webkit 如果 node-webkit 包含支持的版本Node.js 引擎。 (参见 下文。)
SQLite 的 SQLCipher 扩展也支持。 (参见下文。)
Usage
注意:模块必须已安装 a> 使用前。
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database(':memory:');
db.serialize(function() {
db.run("CREATE TABLE lorem (info TEXT)");
var stmt = db.prepare("INSERT INTO lorem VALUES (?)");
for (var i = 0; i < 10; i++) {
stmt.run("Ipsum " + i);
}
stmt.finalize();
db.each("SELECT rowid AS id, info FROM lorem", function(err, row) {
console.log(row.id + ": " + row.info);
});
});
db.close();
Features
- Straightforward query and parameter binding interface
- Full Buffer/Blob support
- Extensive debugging support
- Query serialization API
- Extension support
- Big test suite
- Written in modern C++ and tested for memory leaks
- Bundles Sqlite3 3.26.0 as a fallback if the installing system doesn't include SQLite
API
请参阅 wiki 中的 API 文档。
Installing
你可以使用npm
来 下载 安装:
最新的
sqlite3
包:npm install sqlite3
GitHub的
master
分支:npm install https://github.com/mapbox/node-sqlite3/tarball/master
的模块使用 node-pre-gyp 为您的平台下载预编译的二进制文件(如果存在)。 否则,它使用 node-gyp
构建扩展。
也可以从 它的 源代码而不是 它的 npm 包构建你自己的sqlite3
(见 下面).
可以 使用node-webkit 中安装的包来代替 vanilla Node.js。 有关详细信息,请参阅为 node-webkit 构建。
Source install
要跳过搜索预编译二进制文件并强制从源代码构建,请使用
npm install --build-from-source
The sqlite3 module depends only on libsqlite3。 但是,默认情况下,将构建并静态链接 sqlite 的内部/捆绑副本,因此不需要外部安装的 sqlite3。
如果您希望针对外部 sqlite 安装,则需要将 --sqlite
参数传递给 npm
包装器:
npm install --build-from-source --sqlite=/usr/local
如果针对外部 sqlite3 进行构建,请确保具有开发标头可用的。 Mac OS X 默认附带这些。 如果您没有安装它们,请使用您的包管理器安装 -dev
包,例如 Debian/Ubuntu 的 apt-get install libsqlite3-dev
。 确保您至少有 libsqlite3
>= 3.6。
请注意,如果在 OS X 上针对自制安装的 sqlite 进行构建,您可以执行以下操作:
npm install --build-from-source --sqlite=/usr/local/opt/sqlite/
默认情况下,node-gyp 安装将使用 python
作为安装的一部分。 A 可以在命令行上指定不同的 python 可执行文件。
npm install --build-from-source --python=/usr/bin/python2
这使用了 npmconfigpython 配置,因此 .npmrc 中的值将得到尊重:
python=/usr/bin/python2
Building for node-webkit
由于 ABI 差异,sqlite3
必须在自定义中构建才能与 一起使用 < href="https://github.com/rogerwang/node-webkit">node-webkit。
为 node-webkit 构建 node-sqlite3:
全局安装
nw-gyp
:npm install nw-gyp -g
(除非已经安装)使用
--runtime
、--target_arch< 的自定义标志构建 模块/code> 和
--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)
此命令在内部调用 node-pre -gyp
当--runtime=node-webkit
选项被传递。
您还可以从 node-sqlite3
checkout 中运行此命令:
npm install --build-from-source --runtime=node-webkit --target_arch=ia32 --target=$(NODE_WEBKIT_VERSION)
请记住以下几点:
您必须提供正确的
--target_arch
标志。ia32
需要针对 32 位 node-webkit 构建,而x64
将针对 64 位 node-webkit 构建(如果适用于您的平台)。在为 node-webkit 构建
sqlite3
包后,它不能在普通 Node.js 中运行(反之亦然)。例如,node-webkit 包的
npm test
会失败。
访问 node-webkit wiki 中的“使用 Node 模块”一文了解更多 ;细节。
Building for sqlcipher
有关构建 sqlcipher 的说明,请参见 为 node.js 构建 SQLCipher
运行 node-sqlite3针对 sqlcipher,您需要通过传递构建选项从源代码进行编译,例如:
npm install sqlite3 --build-from-source --sqlite_libname=sqlcipher --sqlite=/usr/
node -e 'require("sqlite3")'
如果您的 sqlcipher 安装在自定义位置(如果您自己编译和安装), 您还需要设置一些环境变量:
On OS X with Homebrew
设置 brew
安装
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)
位置: 设置 make
安装位置:
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
通过 electron-rebuild 不保留 sqlcipher 扩展,因此需要一些额外的标志来使此构建与 Electron 兼容。 您的 npm install sqlite3 --build-from-source
命令需要这些额外的标志(确保将目标版本替换为您正在使用的当前 Electron 版本):
--runtime=electron --target=1.7.6 --dist-url=https://atom.io/download/electron
对于使用 Homebrew 的 MacOS,该命令应如下所示:
npm install sqlite3 --build-from-source --sqlite_libname=sqlcipher --sqlite=`brew --prefix` --runtime=electron --target=1.7.6 --dist-url=https://atom.io/download/electron
Testing
mocha 是运行单元测试所必需的。
在 sqlite3 的目录(它的 package.json
所在的位置)运行以下内容:
npm install mocha
npm test
Contributors
- Konstantin Käfer
- Dane Springmeyer
- Will White
- Orlando Vazquez
- Artem Kustikov
- Eric Fredricksen
- John Wright
- Ryan Dahl
- Tom MacWright
- Carter Thaxton
- Audrius Kažukauskas
- Johannes Schauer
- Mithgol
Acknowledgments
感谢 Orlando Vazquez, Eric Fredricksen 和 Ryan Dahl 用于节点的 SQLite 绑定,以及 Freenode 的 #v8 上的 mraleph 用于回答问题。
此模块的开发由 MapBox 赞助。
License
node-sqlite3
是 BSD 许可。
Asynchronous, non-blocking SQLite3 bindings for Node.js.
Supported platforms
The sqlite3
module works with Node.js v4.x, v6.x, v8.x, v10.x, v11.x and v12.x.
Binaries for most Node versions and platforms are provided by default via node-pre-gyp.
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.)
Usage
Note: the module must be installed before use.
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database(':memory:');
db.serialize(function() {
db.run("CREATE TABLE lorem (info TEXT)");
var stmt = db.prepare("INSERT INTO lorem VALUES (?)");
for (var i = 0; i < 10; i++) {
stmt.run("Ipsum " + i);
}
stmt.finalize();
db.each("SELECT rowid AS id, info FROM lorem", function(err, row) {
console.log(row.id + ": " + row.info);
});
});
db.close();
Features
- Straightforward query and parameter binding interface
- Full Buffer/Blob support
- Extensive debugging support
- Query serialization API
- Extension support
- Big test suite
- Written in modern C++ and tested for memory leaks
- Bundles Sqlite3 3.26.0 as a fallback if the installing system doesn't include SQLite
API
See the API documentation in the wiki.
Installing
You can use npm
to download and install:
The latest
sqlite3
package:npm install sqlite3
GitHub's
master
branch:npm install https://github.com/mapbox/node-sqlite3/tarball/master
The module uses node-pre-gyp to download a pre-compiled binary for your platform, if it exists. Otherwise, it uses node-gyp
to build the extension.
It is also possible to make your own build of sqlite3
from its source instead of its npm package (see below).
It is possible to use the installed package in node-webkit instead of the vanilla Node.js. See Building for node-webkit for details.
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 npmconfigpython config, so values in .npmrc will be honoured:
python=/usr/bin/python2
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:
Install
nw-gyp
globally:npm install nw-gyp -g
(unless already installed)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, whilex64
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://atom.io/download/electron
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://atom.io/download/electron
Testing
mocha is required to run unit tests.
In sqlite3's directory (where its package.json
resides) run the following:
npm install mocha
npm test
Contributors
- Konstantin Käfer
- Dane Springmeyer
- Will White
- Orlando Vazquez
- Artem Kustikov
- Eric Fredricksen
- John Wright
- Ryan Dahl
- Tom MacWright
- Carter Thaxton
- Audrius Kažukauskas
- Johannes Schauer
- Mithgol
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.
Development of this module is sponsored by MapBox.
License
node-sqlite3
is BSD licensed.