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 use the SQLite ZIP VFS to compress a database?
- How can I get the year from a date in SQLite?
- How do I use the SQLite YEAR function?
- How to configure SQLite with XAMPP on Windows?
- How do I decide between using SQLite and MySQL for my software development project?
- How do I show the databases in SQLite?
- How do I use SQLite with Visual Studio?
- How can I use an upsert statement to update data in a SQLite database?
- How do I use SQLite UNION to combine multiple SELECT queries?
- How do I install and use SQLite on Ubuntu?
See more codes...