What is sqlite3?
The sqlite3 npm package is a library that provides a straightforward interface for interacting with SQLite databases in Node.js applications. It allows you to create, read, update, and delete records in SQLite databases, execute SQL queries, and manage database connections.
What are sqlite3's main functionalities?
Create a Database
This code demonstrates how to create an in-memory SQLite database, create a table, and insert some records into it.
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.close();
Query a Database
This code shows how to create a table, insert records, and query the database to retrieve and print the records.
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();
Update Records
This code demonstrates how to update records in an SQLite database.
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.run('UPDATE lorem SET info = ? WHERE rowid = ?', ['Updated Ipsum', 1]);
db.each('SELECT rowid AS id, info FROM lorem', (err, row) => {
console.log(row.id + ': ' + row.info);
});
});
db.close();
Delete Records
This code shows how to delete records from an SQLite database.
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.run('DELETE FROM lorem WHERE rowid = ?', 1);
db.each('SELECT rowid AS id, info FROM lorem', (err, row) => {
console.log(row.id + ': ' + row.info);
});
});
db.close();
Other packages similar to sqlite3
better-sqlite3
better-sqlite3 is a faster and simpler alternative to sqlite3. It provides a more synchronous API, which can be easier to work with in some cases. Unlike sqlite3, better-sqlite3 does not use callbacks and instead returns results directly.
sequelize
Sequelize is a promise-based Node.js ORM for various SQL databases, including SQLite. It provides a higher-level abstraction over SQL queries and supports features like model definition, associations, and migrations. It is more feature-rich compared to sqlite3 but also more complex.
knex
Knex.js is a SQL query builder for Node.js that supports multiple databases, including SQLite. It provides a flexible and powerful API for building and executing SQL queries. Knex.js can be used with or without an ORM and offers more flexibility compared to sqlite3.
NAME
node-sqlite3 - Asynchronous, non-blocking SQLite3 bindings for Node.js 0.2-0.4 (versions 2.0.x), 0.6.13+, 0.8.x, and 0.10.x (versions 2.1.x).
(Can also run in node-webkit if it uses a supported version of Node's engine.)
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
API
See the API documentation in the wiki.
INSTALLING
You can use npm
to download and install:
In both cases the module is automatically built with npm's internal version of node-gyp
,
and thus your system must meet node-gyp's requirements.
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, but a rebuild is required before use (see the next section).
REBUILDING FOR NODE-WEBKIT
Because of ABI differences, only a rebuilt version of sqlite3
can be used in node-webkit.
After the sqlite3
module is installed (according to the previous section), do the following:
-
Install nw-gyp
globally: npm install nw-gyp -g
(unless already installed)
-
Use nw-gyp
to rebuild the module: nw-gyp rebuild --target=0.6.2
Remember the following:
-
In the nw-gyp rebuild
command, specify the actual target version of your node-webkit. The command must be run in sqlite3's directory (where its package.json
resides).
-
After the sqlite3
package is rebuilt 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. - If you need
sqlite3
package both for Node.js and node-webkit, then you should make two separate installations of sqlite3
(in different directories) and rebuild only one of them for node-webkit.
Visit the “Using Node modules” article in the node-webkit's wiki for more details.
BUILDING FROM THE SOURCE
Unless building via npm install
(which uses its own node-gyp
) you will need node-gyp
installed globally:
npm install node-gyp -g
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 node-gyp
, npm install
or the configure
wrapper.
./configure --sqlite=/usr/local
make
Or, using the node-gyp directly:
node-gyp --sqlite=/usr/local
make
Or, using npm:
npm install --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:
./configure --sqlite=/usr/local/opt/sqlite/
make
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
ACKNOWLEDGEMENTS
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.