How To Connect SQLite With Node.js

Node.js

Introduction

 

In this article, we will learn how to use connect SQLite with NodeJS and how to perform the CRUD operations with SQLite and NodeJS.

NodeJS

Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. To know more visit here.

Reference: https://nodejs.org/

SQLITE

SQLite is an in-process library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. The code for SQLite is in the public domain and is thus free for use for any purpose, commercial or private. SQLite is the most widely deployed database in the world with more applications than we can count, including several high-profile projects. It is most widely used for Mobile Apps Development. To know more visit the following link

Reference - https://www.sqlite.org/about.html   

Requisition:

Getting started with node JS, we need to install node JS in our PC or Laptop. We can download Node JS from the following link and install it to your PC.

Reference - https://nodejs.org/en/download/

Steps

I have split this part into two steps as in the following.

Step 1 - Setting up the project with SQLite.

Step 2 - Implementation of CRUD operations.

Step 1 - Setting up the project with SQLite

  1. We will create a project named as “nodejs-sqlite” and in that create a package file as “package.json”. Paste the following in it.
    1. {  
    2.   "name""nodejs-sqlite",  
    3.   "version""1.0.0",  
    4.   "dependencies": {  
    5.     "sqlite3""^ 4.0.0"  
    6.   }  
    7. }  
  1. Here, we have to download the “SQLite3 with version 4.0.0”. To install the dependencies, open command prompt and redirect the terminal to the root directory of the project.

    Node.js
  1. Then type “npm install” and hit enter. This will install the dependencies to node_modules and make your project ready to work.

Step 2 - Implementation of CRUD operations

In this part, we will see how to create DB with SQLite and how to perform CRUD Operations.

  1. The following code will create the SQLite database. We have to create a “JS” file and in my case, I have created the JS File named as js. The file will be run by executing the command “node server.js”.
  2. Then we have to import the SQLite dependency to the file.
    1. var sqlite3 = require('sqlite3').verbose();  
    2. var db = new sqlite3.Database('./mydb.db3');  
    mydb.db3 is automatically created if the DB does not exist in the path.
  1. CREATE TABLE
    The following code shows how to create a table. Normal SQL Query will create the table.
    1. // Create Table  
    2. db.serialize(function() {  
    3.     db.run("CREATE TABLE IF NOT EXISTS Users (Name TEXT, Age INTEGER)");  
    4. });  
  1. INSERT INTO TABLE
    The following code shows how to insert data to table.
    1. // Insert into Table  
    2. db.serialize(function() {  
    3.     db.run("INSERT into Users(Name,Age) VALUES ('Mushtaq',24)");  
    4.     db.run("INSERT into Users(Name,Age) VALUES ('Fazil',23)");  
    5. }  
  1. SELECT FROM TABLE
    The following code shows how to select data from table.
    1. // Select All Data  
    2. db.serialize(function() {  
    3.     db.all("SELECT * from Users",function(err,rows){  
    4.         if(err)  
    5.                              {  
    6.             console.log(err);  
    7.         }  
    8.         else{  
    9.             console.log(rows);  
    10.         }  
    11.     });  
    12. });  
    The above code will show the inserted data from table like the following figure.

    Node.js
  1. UPDATE TO TABLE
    The update operations will be achieved with Normal Transaction SQL run. The following code will show the Update Operation with SQLite.
    1. //Perform UPDATE operation  
    2. db.run("UPDATE table_name where condition");  
  1. DELETE FROM TABLE
    The following code will show the delete Operation with SQLite.
    1. //Perform DELETE operation  
    2. db.run("DELETE * from table_name where condition");  

Download Code

If you found this article to be informative, do like and star the repo on GitHub. You can download the full sample code from GitHub.


Similar Articles