Node.js MySQL UPDATE

In this post, we will discuss how to delete rows from MySQL XAMPP Server using Node.js

Command to install the mysql package:

CopiedCopy Code
npm install mysql
				

UPDATE Statement

UPDATE is used to update row/s present in a table. It is important to specify the condition along with this command. Condition can be specified using WHERE Clause.

Steps:

Now let's see steps

  1. First start your XAMPP Server (Both Apache and MySQL).
  2. Open Notepad or any text-editor and write the Node.js script
  3. In that script, first we have to load the mysql package using the below syntax
  4. var mysql_package = require('mysql');
  5. Create the connection using the server,username and password.
  6. CopiedCopy Code
    
    var connection_data = mysql_package.createConnection({
      host: "localhost",
      user: "root",
      password: "",
      database:"database_name"
    });
    
  7. Write the sql query that uses UPDATE command
  8. CopiedCopy Code
    
    connection_data.connect(function(error) {
      var sql_query = "UPDATE table_name SET column1=value,column2=value,.... 
      WHERE conditions....";
      connection_data.query(sql_query, function (error, result) {
    	  
        console.log("Row/s updated...");
    });
    });
    
  9. Now type the following command in your command prompt to run the script.
  10. node file_name.js

Consider the village table with the following records:

alt=

UPDATE Example 1:-

Let's update vname to Hyd in village table where vid is 1.

CopiedCopy Code

// Load the mysql package
var mysql_package = require('mysql');
// Create the connection using the server,username and password.
//In my scenario - server is the localhost,
//username is root,
//password is empty.
//database is facility
var connection_data = mysql_package.createConnection({
  host: "localhost",
  user: "root",
  password: "",
  database:"facility"
});
connection_data.connect(function(error) {
	// Query to update vname to Hyd if vid=1.
  var sql_query = "UPDATE village SET vname = 'Hyd' WHERE vid = 1";
  connection_data.query(sql_query, function (error, result) {
    console.log(result.affectedRows + " row updated successfully");
  });
});
Output:
CopiedCopy Code

1 row updated successfully

Let's check in our XAMPP Server whether the rows updated or not.

alt=

UPDATE Example 2:-

Let's update vname to other in village table where people is greater than 50.

CopiedCopy Code

// Load the mysql package
var mysql_package = require('mysql');

// Create the connection using the server,username and password.
//In my scenario - server is the localhost,
//username is root,
//password is empty.
//database is facility
var connection_data = mysql_package.createConnection({
  host: "localhost",
  user: "root",
  password: "",
  database:"facility"
});
connection_data.connect(function(error) {
	// Query to update vname to other if people> 50.
  var sql_query = "UPDATE village SET vname = 'other' WHERE people>50";
  connection_data.query(sql_query, function (error, result) {
    console.log(result.affectedRows + " rows updated successfully");
  });
});
Output:
CopiedCopy Code

3 rows updated successfully

Let's check in our XAMPP Server whether the rows updated or not.

alt= Summary

In this post, we seen how to update records using UPDATE Command in XAMPP Server with two Node.js scripts.