Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "root",
database: "Employee"
});
con.connect(function (err) {
if (err) throw err;
console.log('Connected');
});
var getEmpObj = function (empName, callback) {
let seletQuery = "SELECT * FROM Employee where employeeName = '" + empName + "'";
con.query(seletQuery, function (err, result, fields) {
if (err) throw err;
callback(result);
});
}
var updateEmpAddress = function (empName, empNewAddress, callback) {
getEmpObj(empName, function(result) {
let empId = result[0].employeeId;
let updateAddressQuery = "UPDATE Employee SET employeeAddress = '" + empNewAddress + "' WHERE employeeId = " + empId;
con.query(updateAddressQuery, function (err, result) {
if (err) throw err;
console.log(result.affectedRows + " record(s) updated");
callback();
});
});
}
var printEmployeeDetails = function(empName) {
getEmpObj(empName, function(result) {
console.log(JSON.stringify(result[0]));
con.end();
});
}
updateEmpAddress("Aditya", "Delhi", printEmployeeDetails("Aditya"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment