If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
MySQL table में records update करने के लिए आपको बस MySQL query में ही changes करने होंगे। बाकी सब same ही है।
// import mysql package
const mysql_module = require('mysql');
const mysql = mysql_module.createConnection({
host: "localhost",
user: "root",
password: null,
database : "node_mysql"
});
// connect database.
mysql.connect(function(err) {
if (err) throw err
// update data where id id 1.
let query = `UPDATE tbl_users set first_name='Rahul Kumar', last_name='Rajput' WHERE id=1`
mysql.query(query, (error, result) => {
if (error) throw error
console.log("record updated : ", result);
})
});
C:\Users\HP\Desktop\workspace\nodejs>node app.js PS D:\workspace\nodejs> node app.js record updated : OkPacket { fieldCount: 0, affectedRows: 1, insertId: 0, serverStatus: 2, warningCount: 0, message: '(Rows matched: 1 Changed: 1 Warnings: 0', protocol41: true, changedRows: 1 }
Example में ID के bases पर record को update किया गया है। और Output में देख सकते हैं कि affectedRows: 1 है means record update हो चुका है।
हालाँकि अगर आप चाहे तो column : value की जगह simply question mark (?) भी पास कर सकते हैं। जैसा कि नीचे example में दिखाया गया है।
const mysql_module = require('mysql');
const mysql = mysql_module.createConnection({
host: "localhost",
user: "root",
password: null,
database : "node_mysql"
});
mysql.connect(function(err) {
if (err) throw err
// update data where id id 2.
let update_obj = {
first_name : 'Rohan Kumar',
last_name : 'Sharma'
}
let id = 2
let query = `UPDATE tbl_users set ? WHERE id=?`
mysql.query(query, [update_obj, id], (error, result) => {
if (error) throw error
console.log("record updated : ", result);
})
});
C:\Users\HP\Desktop\workspace\nodejs>node app.js PS D:\workspace\nodejs> node app.js record updated : OkPacket { fieldCount: 0, affectedRows: 1, insertId: 0, serverStatus: 2, warningCount: 0, message: '(Rows matched: 1 Changed: 1 Warnings: 0', protocol41: true, changedRows: 1 }
I Hope, आपको समझ में आया होगा कि Node.js में MySQL records को कैसे update करते हैं।
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook