sqliteHow do I use SQLite with npm?
Using SQLite with npm
SQLite can be used with npm by installing the sqlite3
npm package. This package provides an asynchronous, non-blocking SQLite3 bindings for Node.js.
To install the package, run the following command:
npm install sqlite3
Once the package is installed, you can use it in your Node.js application. The following example code demonstrates how to create a database and run a simple query:
// create a database
const sqlite3 = require('sqlite3');
const db = new sqlite3.Database('example.db');
// run a query
db.run("CREATE TABLE IF NOT EXISTS people (name TEXT, age INTEGER)");
db.run("INSERT INTO people (name, age) VALUES ('John', 25)");
// get the results
db.all("SELECT * FROM people", (err, rows) => {
console.log(rows);
});
The output of this code will be:
[ { name: 'John', age: 25 } ]
The code consists of the following parts:
sqlite3
: the npm packagedb
: the database objectdb.run
: the method to run a querydb.all
: the method to get the results of a query
For more information, see the sqlite3 npm package documentation.
More of Sqlite
- How do I use variables in a SQLite database?
- How do I store a timestamp in an SQLite database?
- How do I use the SQLite zfill function?
- How do I use SQLite transactions?
- How can I use SQLite with Xamarin and C# to develop an Android app?
- How do I set up an ODBC driver to connect to an SQLite database?
- How do I show the databases in SQLite?
- How can I use SQLite online?
- How do I install SQLite?
- How do I use SQLite to retrieve data from a specific year?
See more codes...