2

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 = ?  
9
  • Can you show the update_Data and cond objects? Commented Jun 29, 2019 at 8:39
  • @Molda this is my update data object [ [ { upin_id: 1, media_type: 5, land: 'Rakhiyal Circle' } ], [ { upin_id: 3, media_type: 6, land: 'Rakhiyal Circle' } ] ] and this is cond bid_id=1 Commented Jun 29, 2019 at 8:47
  • I don't think you can do multiple updates this way. See this answer stackoverflow.com/a/29519758/3284355 Commented Jun 29, 2019 at 8:59
  • @Molda I have already seen this answer.is that right way?? Commented Jun 29, 2019 at 9:02
  • I don't know. You can also try this stackoverflow.com/a/34866431/3284355 which might be better since it is only single query. Commented Jun 29, 2019 at 9:08

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.