How to Connect SQLite3 Database using Node.js ?
Last Updated :
20 Jun, 2024
Connecting SQLite3 database with Node.js involves a few straightforward steps to set up and interact with the database. SQLite is a self-contained, serverless, zero-configuration, transactional SQL database engine, making it ideal for small to medium-sized applications. Here’s how you can connect and work with SQLite3 using Nodejs:
Approach
To connect to an SQLite3 database in Node.js, install the sqlite3
package, then use it to create a database connection. Execute SQL queries using the db.run
, db.get
, or db.all
methods for inserting, retrieving, and managing data.
Installation Steps
Step 1: Create a folder application by using this command
mkdir myapp
Step 2: Navigate to the project directory
cd myapp
Step 3: Initialize Node Project
NPM, Create and Locate your project folder in the terminal & type the command
npm init -y
Step 4: Install the necessary packages/libraries in your project using the following commands.
npm install express sqlite3
Project Structure:

The updated dependencies in package.json file will look like:
"dependencies": {
"express": "^4.19.2",
"sqlite3": "^5.1.7"
}
Example: Implementation to create express server which renders GeeksforGeeks.
Node
// filename index.js
const express = require('express');
const app = express();
app.get('/' , (req , res)=>{
res.send("GeeksforGeeks");
})
app.listen(4000 , ()=>{
console.log("server started");
})
Output:

Importing sqlite3 into our project
Syntax:
const sqlite3 = require('sqlite3');
Example: Implementation to use Database method which is available in sqlite3 which helps us to connect with database.
Node
// filename - index.js
const express = require('express');
const app = express();
const sqlite3 = require('sqlite3');
// Connecting Database
let db = new sqlite3.Database(":memory:" , (err) => {
if(err)
{
console.log("Error Occurred - " + err.message);
}
else
{
console.log("DataBase Connected");
}
})
app.get("/" , (req , res) => {
res.send("GeeksforGeeks");
})
// Server Running
app.listen(4000 , () => {
console.log("Server started");
})
Step to Run Server: Run the server using the following command from the root directory of the project:
node index.js
Output:

Additional Tips
- Use Database Transactions: SQLite supports transactions (
BEGIN TRANSACTION
, COMMIT
, ROLLBACK
) for handling multiple operations atomically. - Parameterized Queries: Use parameterized queries (
?
placeholders) to prevent SQL injection attacks and ensure secure data handling. - Async/Await with SQLite3: You can also use
async
/await
syntax with SQLite3 operations for cleaner and more readable asynchronous code.
Conclusion
Connecting SQLite3 database with Node.js involves setting up the database connection, performing operations, and handling errors effectively. SQLite is lightweight, efficient, and suitable for applications that require a simple, embedded database solution. By following the steps and examples outlined above, you can start building robust Node.js applications with SQLite as the backend database.
Similar Reads
How to Create Table in SQLite3 Database using Node.js ? Creating a table in an SQLite database using Node.js involves several steps, including setting up a connection to the database, defining the table schema, and executing SQL commands to create the table. SQLite is a lightweight and serverless database that is widely used, especially in applications t
3 min read
How to Connect to a MongoDB Database Using Node.js MongoDB is a NoSQL database used to store large amounts of data without any traditional relational database table. To connect to a MongoDB database using NodeJS we use the MongoDB library "mongoose". Steps to Connect to a MongoDB Database Using NodeJSStep 1: Create a NodeJS App: First create a NodeJ
4 min read
How to update data in sqlite3 using Node.js ? In this article, we are going to see how to update data in the sqlite3 database using node.js. So for this, we are going to use the run function which is available in sqlite3. This function will help us to run the queries for updating the data.SQLite is a self-contained, high-reliability, embedded,
4 min read
How to Connect to a MongoDB Database Using the Node.js Driver ? MongoDB is a popular, open-source, NoSQL (non-relational) database that provides high performance, high availability, and easy scalability. Unlike traditional relational databases, MongoDB stores a JSON-like format called BSON (Binary JSON). In this article, we connect the MongoDB database to your b
4 min read
How to create new Mongodb database using Node.js ? mongodb module: This Module is used to performing CRUD(Create Read Update Read) Operations in MongoDb using Node.js. We cannot make a database only. We have to make a new Collection to see the database. The connect() method is used for connecting the MongoDb server with the Node.js project. Please r
1 min read
How to Insert and Select Data in SQLite3 Database using Node.js ? Inserting and selecting data in an SQLite3 database using Node.js involves connecting to the database, running SQL queries, and handling the results. SQLite is an excellent choice for small to medium-sized applications due to its simplicity and lightweight nature. This guide will walk you through th
3 min read
How to Connect Node to a MongoDB Database ? Connecting Node.js to MongoDB is a common task for backend developers working with NoSQL databases. MongoDB is a powerful, flexible, and scalable database that stores data in a JSON-like format. In this step-by-step guide, we'll walk through the entire process from setting up your development enviro
6 min read
How to Connect to a MySQL Database Using the mysql2 Package in Node.js? We will explore how to connect the Node.js application to a MySQL database using the mysql2 package. MySQL can be widely used as a relational database and mysql2 provides fast, secure, and easy access to MySQL servers, it can allow you to handle database queries efficiently in Node.js applications.
6 min read
How to drop database of MongoDB using Node.js ? MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term âNoSQLâ means ânon-relationalâ. It means that MongoDB isnât based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data. This fo
2 min read
How to create new Collection in MongoDB using Node.js ? MongoDB the most popular NoSQL database, is an open-source document-oriented database. The term âNoSQLâ means ânon-relationalâ. It means that MongoDB isnât based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data. This for
1 min read