Open In App

How to insert and select data in sqlite3 database using node.js ?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see how to insert and select data in sqlite3 database using node.js. So for this, we are going to use the run and all function which is available in sqlite3.

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.

Below is the step by step implementation:

Step 1: Setting up the NPM package of the project using the following command:

npm init -y

Step 2: Install Dependencies using the following command:

npm install express sqlite3

Project structure: It will look like the following.

Step 3:  Here, we created a basic express server that renders GeeksforGeeks on the browser screen.

index.js




const express = require('express');
const app = express();
       
app.get('/' , (req , res)=>{
    res.send("GeeksforGeeks");
});
      
app.listen(4000 , ()=>{
    console.log("server started");
});


Step 4: 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.

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



Last Updated : 16 Mar, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads