sqliteHow can I use SQLite with Node.js?
SQLite is a popular, open-source, lightweight relational database system. It is often used with Node.js due to its simple setup and ease of use. To use SQLite with Node.js, you must first install the sqlite3 module.
$ npm install sqlite3
Once the module is installed, you can create a database connection and perform SQL queries. The following example code creates a database connection and executes a query to create a table.
const sqlite3 = require('sqlite3').verbose();
let db = new sqlite3.Database(':memory:', (err) => {
if (err) {
return console.error(err.message);
}
console.log('Connected to the in-memory SQlite database.');
});
db.run('CREATE TABLE test_table (name TEXT)', (err) => {
if (err) {
return console.error(err.message);
}
console.log('Table created successfully.');
});
Output example
Connected to the in-memory SQlite database.
Table created successfully.
The code above:
- Imports the
sqlite3
module (const sqlite3 = require('sqlite3').verbose();
) - Creates a database connection (
let db = new sqlite3.Database(':memory:', (err) => {
) - Executes a query to create a table (
db.run('CREATE TABLE test_table (name TEXT)', (err) => {
)
For more information, see the sqlite3 documentation and the Node.js documentation.
More of Sqlite
- How do I store a timestamp in an SQLite database?
- How to configure SQLite with XAMPP on Windows?
- How can I use SQLite with Zabbix?
- How can I use SQLite with Xamarin?
- How can SQLite and ZFS be used together for software development?
- How can I use Python to update a SQLite database?
- How can I use SQLite to query for records between two specific dates?
- How do I show the databases in SQLite?
- How do I use the SQLite Workbench?
- How do I use the SQLite SUBSTRING function?
See more codes...