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 SQLite with Zephyr?
- How do I list all tables in a SQLite database?
- How do I download and install SQLite zip?
- How do I set up an ODBC driver to connect to an SQLite database?
- How do I use the SQLite ZIP VFS to compress a database?
- How do I format a date in SQLite using the YYYYMMDD format?
- How do I use SQLite xfilter to filter data?
- How to configure SQLite with XAMPP on Windows?
- How can I use SQLite to query for records between two specific dates?
- How do I use the SQLite sequence feature?
See more codes...