简体   繁体   English

使用mysql.js执行UPDATE语句

[英]Executing UPDATE statement with mysqljs

Im using mysqljs and I'm trying to UPDATE a table but it shows me an error with my SQL sentence. 我正在使用mysqljs并且正在尝试更新表,但它向我显示SQL语句错误。

the documentation states that 文件指出

connection.query('UPDATE users SET foo = ?, bar = ?, baz = ? WHERE id = ?', ['a', 'b', 'c', userId], function(err, results) {
  // ...
});

But when I try to do it, the query creates all the variables that I'm sending to the first ?, ignorign the other ones, like this 但是,当我尝试执行此操作时,查询将创建要发送给第一个?的所有变量,忽略其他变量,如下所示

values = [1,2,3,4,5]
sql = 'UPDATE tablename SET col1= ?, col2 = ?, col3= ?, col4 = ? WHERE col5=  ?';

  var query = connection.query(sql, [values], function(err) {
      if (err) {
        console.log(err);
        throw err;
      }
      else {
        connection.end();
      }
  })

But the query that gets executes is: 但是执行的查询是:

UPDATE tablename SET col1= 1, 2, 3, 4, col2 = ?, col3= ?, col4 = ? WHERE col5=  ?';

I don't know what I'm doing wrong or what to do to fix it. 我不知道自己在做错什么或如何解决。

You aren't passing an array of values, you are passing an array of an array of values. 您没有传递值数组,而是传递值数组的数组 Stop doing that. 别那样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM