26

I trying to update some info in MYSQL DB, but I'm not sure of how to do it with in node.js. This is the mysql driver I'm using https://github.com/felixge/node-mysql

What I have so far

connection.query('SELECT * FROM users WHERE UserID = ?', [userId], function(err, results) {
if (results[0]) {
if (results[0].Name!=name) {
console.log(results[0].Name);
connection.query('UPDATE users SET ? WHERE UserID = ?', [userId], {Name: name});
}
console.log(results[0].UserID);
}
});

Everything works except...

connection.query('UPDATE users SET ? WHERE UserID = ?', [userId], {Name: name});

In PHP I would have this...

mysql_query("UPDATE users SET Name='".$_GET["name"]."' WHERE UserID='".$row['UserID']."'");

I'm not sure what I'm doing wrong, But I'm positive that the issue is here

connection.query('UPDATE users SET ? WHERE UserID = ?', [userId], {Name: name});
4

5 回答 5

50

答案ruakh并不完全正确,您可以将该SET ?功能与另一个?.

语法是:

connection.query('UPDATE users SET ? WHERE UserID = ?', [{ Name: name }, userId])
于 2013-10-14T10:57:39.133 回答
49

[注(2016-04-29添加):这个答案被接受了,但事实证明有一种合理的使用方式SET ?。有关详细信息,请参阅同一页面上的 Bala Clark 的回答。——鲁阿赫]


and的代码中Connection.prototype.query()Connection.createQuery(),很明显您只能传入单个值对象。我没有看到特殊SET ?行为的代码是在哪里定义的——它显然不在SqlString.formatQuery()——但如果它使用SqlString.objectToValues(),那么我想没有办法将它与另一个一起使用?

我认为最好的方法是放弃整洁的SET ?功能,并编写以下任何一个:

connection.query('UPDATE users SET Name = ? WHERE UserID = ?', [name, userId])
connection.query('UPDATE users SET Name = :Name WHERE UserID = :UserID',
                     {UserID: userId, Name: name})

但如果你真的想使用SET ?,我想你可以这样写:

connection.query('UPDATE users SET ? WHERE UserID = :UserID',
                     {UserID: userId, Name: name})

这将同时更新UserIDName;除非您有触发器,否则应该没问题,因为它无论如何都会更新UserID到它已经拥有的值。但这有点令人不安,我不推荐它。

于 2013-02-21T01:33:37.433 回答
22

您可以使用对象数组:

connection.query('UPDATE user SET ? WHERE ?', [{ Name: name }, { UserId: userId }])
于 2014-04-02T08:57:59.543 回答
0

这对我有用

 var records = 
 [name,email,adhar,ROLE,NOTES,REQUESTFORSUPPLIER,PASSWORD,LOCATION,CREATEDBY,CREATEDON,MODIFIEDBY,MODIFIEDON,mobile];

    var sql="UPDATE flavica_user SET Name=?,EMAIL=?,AADHAR=?,ROLE=?,NOTES=?,REQUESTFORSUPPLIER=?,PASSWORD=?,LOCATION=?,CREATEDBY=?,CREATEDON=?,MODIFIEDBY=?,MODIFIEDON=? WHERE USER_MOBILE=?"
    //var sql="UPDATE `flavica_user` SET ? WHERE ?";
    //this sql updates flavica_user
    con.query(sql,records,(err,result)=>{

      if(err){
        res.json({
          status:400,
          message:err
        })
      }

      else{
        res.json({
          status:200,
          message:result
        })
      }

    })
于 2018-09-22T07:18:17.527 回答
0

在此处输入图像描述

let query_1 =  `UPDATE reindexing_status SET success="Failure" where status="In Progress"`;
await updateValues(db_connection,query_1);
 
if(lambdaInvokeStatusCode === 200)

{
   let query_1 =  `UPDATE reindexing_status SET success="Success" where status="In Progress"`;
   await updateValues(db_connection,query_1);
   
   let query_2 =  `UPDATE reindexing_status SET end_time='${indexedAt}' where status="In Progress"`;
   await updateValues(db_connection,query_2);
   
   let query_3 =  `UPDATE reindexing_status SET status="Complete" where status="In Progress"`;
   await updateValues(db_connection,query_3);
   

}



async function updateValues(db_connection,sql) {
await new Promise((resolve, reject) => {
    db_connection.query(sql, function (err, result) {
        if (err) {
            console.log(err);
            reject(err);
        }
        else {
           
            console.log("RDS Updated Successfully",result );
            resolve("RDS URL Column: " + JSON.stringify(result));
        }
    });
 
});
}
于 2021-04-16T02:59:44.173 回答