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 install and use SQLite x64 on my computer?
- How do I use SQLite to update or insert data?
- How can I use SQLite with Python?
- How can I use SQLite with Xamarin Forms and C#?
- How do I use SQLite UNION to combine multiple SELECT queries?
- How do I use the SQLite on delete cascade command?
- How do I use SQLite transactions?
- How do I rename a table in SQLite?
- How do I set up an ODBC driver to connect to an SQLite database?
- How do I use regular expressions to query a SQLite database?
See more codes...