- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Updating a record in MySQL using NodeJS
In this article, we will see how we can update a record in MySQL using NodeJS. We will dynamically update MySQL table values from Node.js server. You can use the select statement after updating to check if the MySql record is updated.
Before proceeding, please check the following steps are already executed −
mkdir mysql-test
cd mysql-test
npm init -y
npm install mysql
The above steps are for installing the Node - mysql dependecy in the project folder.
Upadting a Record into the Students Table −
For updating an existing record into the MySQL table, firstly create an app.js file
Now copy-paste the below snippet in the file
Run the code using the following command
>> node app.js
Example
// Checking the MySQL dependency in NPM var mysql = require('mysql'); // Creating a mysql connection var con = mysql.createConnection({ host: "localhost", user: "yourusername", password: "yourpassword", database: "mydb" }); con.connect(function(err) { if (err) throw err; var sql = "UPDATE student SET address = 'Bangalore' WHERE name = 'John';" con.query(sql, function (err, result) { if (err) throw err; console.log(result.affectedRows + " Record(s) updated."); console.log(result); }); });
Output
1 Record(s) updated. OkPacket { fieldCount: 0, affectedRows: 1, // This will return the number of rows updated. insertId: 0, serverStatus: 34, warningCount: 0, message: '(Rows matched: 1 Changed: 1 Warnings: 0', // This will return the number of rows matched. protocol41: true, changedRows: 1 }
Example
// Checking the MySQL dependency in NPM var mysql = require('mysql'); // Creating a mysql connection var con = mysql.createConnection({ host: "localhost", user: "yourusername", password: "yourpassword", database: "mydb" }); con.connect(function(err) { if (err) throw err; // Updating the fields with address while checking the address var sql = "UPDATE student SET address = 'Bangalore' WHERE address = 'Delhi';" con.query(sql, function (err, result) { if (err) throw err; console.log(result.affectedRows + " Record(s) updated."); console.log(result); }); });
Output
3 Record(s) updated. OkPacket { fieldCount: 0, affectedRows: 3, // This will return the number of rows updated. insertId: 0, serverStatus: 34, warningCount: 0, message: '(Rows matched: 3 Changed: 3 Warnings: 0', // This will return the number of rows matched. protocol41: true, changedRows: 3 }
- Related Articles
- Dropping a MySQL Table using NodeJS
- Deleting records in MySQL using Nodejs
- Creating a MySQL Table in NodeJS using Sequelize
- “Where” clause not working while updating database record in ABAP
- Insert record using MySQL SELECT?
- Updating boolean value in MySQL?
- Updating only a single column value in MySQL?
- Updating last entry of a particular ID in MySQL
- Search record with a specific value in MySQL using LIKE or REGEXP?
- How can you delete a record from a table using MySQL in Python?
- Get digits from a record in MySQL?
- Delete a specific record from a MySQL table by using AND in WHERE clause
- Updating a MySQL column that contains dot (.) in its name?
- How to use a select statement while updating in MySQL?
- Sync Copy in fs-extra using NodeJS

Advertisements