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 can I use an upsert statement to update data in a SQLite database?
- How can I use SQLite online?
- How do I use SQLite with Zephyr?
- How do I use regular expressions to query a SQLite database?
- How do I use the SQLite ZIP VFS to compress a database?
- How can I use SQLite to query for records between two specific dates?
- How can SQLite and ZFS be used together for software development?
- How can I use SQLite with Python to create a database?
- How do I use the SQLite zfill function?
- How do I extract the year from a datetime value in SQLite?
See more codes...