How to Insert and Select Data in SQLite3 Database using Node.js ?
Last Updated :
31 Jul, 2024
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 the steps to insert and select data from an SQLite3 database using Node.js.
SQLite
SQLite is a self-contained, high-reliability, embedded, public-domain, SQL database engine. It is the most used database engine in the world. Let’s understand how to create a table in a SQLite3 database using Node.js.
Steps to Setup Project
Step 1: Make a folder structure for the project.
mkdir myapp
Step 2:Â Navigate to the project directory
cd myapp
Step 3: Initialize the NodeJs project inside the myapp folder.
npm init -y
Step 4: Install the necessary packages/libraries in your project using the following commands.
npm install express sqlite3
Project Structure:
Project Structure | How to Insert and Select Data in SQLite3 Database using Node.jsThe updated dependencies in package.json file will look like:
"dependencies": {
"express": "^4.19.2",
"sqlite3": "^5.1.7",
}
Example: Implementatin to first create a server
Node
// 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");
// Query
var createQuery =
'CREATE TABLE GFG ( ID NUMBER , NAME VARCHAR(100));';
var insertQuery =
'INSERT INTO GFG (ID , NAME) VALUES (1 , "GeeksforGeeks");'
var selectQuery = 'SELECT * FROM GFG ;'
// Running Query
db.run(createQuery , (err) => {
if(err) return;
// Success
console.log("Table Created");
db.run(insertQuery , (err) => {
if(err) return;
// Success
console.log("Insertion Done");
db.all(selectQuery , (err , data) => {
if(err) return;
// Success
console.log(data);
});
});
});
})
Step 5: Importing 'sqlite3' into our project using the following syntax. There are lots of features in the sqlite3 module.
const sqlite3 = require('sqlite3');
Step 5: Now write a query for inserting and selecting data in sqlite3.
/* Here GFG is table name */ var insertQuery = 'INSERT INTO GFG (ID , NAME) VALUES (1 , "GeeksforGeeks");' var selectQuery = 'SELECT * FROM GFG ;' /* Here GFG is table name */
Step 6: Here we are going to use a Run and All method which is available in sqlite3.
Node
// 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");
// Query
var createQuery =
'CREATE TABLE GFG ( ID NUMBER , NAME VARCHAR(100));';
var insertQuery =
'INSERT INTO GFG (ID , NAME) VALUES (1 , "GeeksforGeeks");'
var selectQuery = 'SELECT * FROM GFG ;'
// Running Query
db.run(createQuery , (err) => {
if(err) return;
// Success
console.log("Table Created");
db.run(insertQuery , (err) => {
if(err) return;
// Success
console.log("Insertion Done");
db.all(selectQuery , (err , data) => {
if(err) return;
// Success
console.log(data);
});
});
});
})
Step to Run Server: Run the server using the following command from the root directory of the project:
node index.js
Output:
Reference: https://www.npmjs.com/package/sqlite3
Similar Reads
How to Connect SQLite3 Database using Node.js ? 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 an
2 min read
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 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 insert single and multiple documents 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 fo
2 min read
How to Add Data in JSON File using Node.js ? JSON stands for Javascript Object Notation. It is one of the easiest ways to exchange information between applications and is generally used by websites/APIs to communicate. For getting started with Node.js, refer this article.Prerequisites:NPM NodeApproachTo add data in JSON file using the node js
4 min read
How to insert request body into a MySQL database using Express js If you trying to make an API with MySQL and Express JS to insert some data into the database, your search comes to an end. In this article, you are going to explore - how you can insert the request data into MySQL database with a simple Express JS app.Table of Content What is Express JS?What is MySQ
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 add records in your own local/custom database in Node.js ? The custom database signifies the local database in your file system. There are two types of database 'SQL' and 'NoSQL'. In SQL database data are stored as table manner and in Nosql database data are stored independently with some particular way to identify each record independently. We can also cre
3 min read
How to update a record in your local/custom database in Node.js? The custom database signifies the local database in your file system. There are two types of database âSQLâ and âNoSQLâ. In SQL database, data are stored as table manner and in Nosql database data are stored independently with some particular way to identify each record independently. We can also cr
4 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