In this tutorial you will learn about the Nodejs MySQL Update Records and its application with practical example.
Node.js MySQL Update Records
Database record can be updated using the “UPDATE” statement.
Example :- Let’s update “emp_salary” in “employees” table where “id” is 5.
Step 1:- Let’s, create a node_mysql_update_tbl.js file and put the following code in it –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
var mysql = require('mysql'); var con = mysql.createConnection({ host: "localhost", user: "root", password: "", database: "nodemysql" }); con.connect(function(err) { if (err) throw err; var sql = "UPDATE employees SET emp_salary = '8000' WHERE id = 5"; con.query(sql, function (err, result) { if (err) throw err; console.log("W3Adda Mysql Record Updated"); console.log(result.affectedRows + " record(s) updated"); }); }); |
Step 2:- Save the code and open the terminal again, and type the following command in order to run the file.
1 |
$ node node_mysql_update_tbl.js |
you will see following output on terminal –
![Node.js MySQL Update Records 1](http://w3adda.com/wp-content/uploads/2018/04/Screenshot_1-2.jpg)
Node.js MySQL Update Records 1
Before Update –
![Node.js MySQL Update Records 2](http://w3adda.com/wp-content/uploads/2018/04/Screenshot_2-1.jpg)
Node.js MySQL Update Records 2
After Update –
![Node.js MySQL Update Records 3](http://w3adda.com/wp-content/uploads/2018/04/Screenshot_3-1.jpg)
Node.js MySQL Update Records 3