Reputation: 365
I am facing problem while updating multiple rows in table.
following is my data
const update_Data = upins_data.map(
upins_data => [{
upin_id: upins_data.upin_id,
media_type: upins_data.media_type,
land: upins_data.land
}]
);
it has multiple values.
i have tired following code
var updateSecTab = `UPDATE tb_bid_upins SET upin_id=?,media_type=?,land=? WHERE bid_id = ?`;
var query = db.query(updateSecTab,[update_Data,cond],function(error,result,fields){
if (error) { /**If error while inserting into table 2 it rowback the Transaction */
console.log('in err 1');
return db.rollback(function () {
throw err;
});
}
db.commit(function (err, result3) {
if (err) {
console.log('in err 2');
return db.rollback(function () {
throw err;
});
}
/**Send success result to front end */
console.log('success!');
res.send(JSON.stringify(result));
})
});
console.log(query.sql);
When i print the query it gives result as follows
UPDATE tb_bid_upins SET upin_id=('[object Object]'), ('[object Object]'),media_type=1,land=? WHERE bid_id = ?
Upvotes: 2
Views: 1161
Reputation: 365
Hello everyone i have tried following solution and it works.
var upins_data = [ { upin_id: 1, media_type: 5, land: 'Rakhiyal Circle' } ],
[ { upin_id: 3, media_type: 6, land: 'Rakhiyal Circle' } ]
var cond = 1
i have added above two variable in single array as follows
const update_Data = upins_data.map(
upins_data => {
return {
bid_id: cond,
upin_id: upins_data.upin_id,
media_type: upins_data.media_type,
land: upins_data.land
}
}
);
than i have used foreach on update_Data array and i have created multiple sql quires using mysql foramt function.
var queries = '';
update_Data.forEach(function (item) {
let sql = mysql.format("UPDATE tb_bid_upins SET upin_id=?,media_type=?,land=? WHERE bid_id = ? ;", [item.upin_id,item.media_type, item.land, item.bid_id]);
queries += sql;
console.log(sql);
});
than i have passed above queries variable in sql query as follows
db.query(queries,(err,result)=>{
if(err){
console.log('err',err);
}else{
console.log('result',result);
}
});
above code works for me in nodejs .
if anyone wants to improve than they can. thank you
Upvotes: 2