How to update data in sqlite3 using Node.js ?

In this write-up, we will explore the process of updating data within a SQLite3 database using Node.js. To accomplish this task, we will utilize the 'run' function provided by the SQLite3 library. This 'run' function plays a crucial role in executing queries aimed at modifying existing data in the database.

SQLite is a widely adopted, self-contained, high-reliability, public-domain SQL database engine, and it is the most extensively utilized database engine globally. In this article, we'll explore the process of updating data in an 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
 

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 updating data in sqlite3.

/* Here GFG is table name */
var updateQuery = 'UPDATE GFG SET NAME = "Updated_Name"';

Step 6: Here we are going to use a Run method which is available in sqlite3.

index.js
 

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

No comments:

Post a Comment