Example update record in mysql with nodejs

Update Table

You can update existing records in a table by using the "UPDATE" statement

Example: Overwrite the address column from "Valley 345" to "Canyon 123"

 

var mysql = require('mysql');

var con = mysql.createConnection({

  host: "localhost",

  user: "root",

  password: "",

  database: "mydb"

});

con.connect(function(err) {

  if (err) throw err;

  //Update the address field:

  var sql = "UPDATE customers SET address = 'Canyon 123' WHERE address = 'Valley 345'";

  con.query(sql, function (err, result) {

    if (err) throw err;

    console.log(result.affectedRows + " record(s) updated");

    console.log(result);

  });

});

 

Run and result:

 

C:\Nodejs>node mysqlUpdateRecordData.js

1 record(s) updated

OkPacket {

  fieldCount: 0,

  affectedRows: 1,

  insertId: 0,

  serverStatus: 34,

  warningCount: 0,

  message: '(Rows matched: 1  Changed: 1  Warnings: 0',

  protocol41: true,

  changedRows: 1 }