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 can SQLite and ZFS be used together for software development?
- How do I use SQLite to retrieve data from a specific year?
- How to configure SQLite with XAMPP on Windows?
- How can I use SQLite with WPF?
- How do I use SQLite with Visual Studio?
- How do I use SQLite with Zephyr?
- How do I troubleshoot a near syntax error when using SQLite?
- How do I use the SQLite ZIP VFS to compress a database?
- How can I use SQLite with Zabbix?
- How do I extract the year from a datetime value in SQLite?
See more codes...