【问题标题】:Nodejs multiple sql query loopNodejs多个sql查询循环
【发布时间】:2016-10-16 05:56:53
【问题描述】:

我对 nodejs 和异步世界还很陌生。

情况是,我有一个像var ids = [1, 2, 3, 4]这样的数组;

我需要根据数组元素的顺序更新mytable。所以我做了类似的事情:

sort: function(ids, callback) {

 // dont worry about this
  this.create_connection();
  this.connection.connect();

   for(var i=0; i<ids.length;i++) {
      var q = "UPDATE mytable SET sequence="+i+" where id="+ids[i]+"; ";

      this.connection.query(q, function(err, result) {

       // I am not sure about this
       // callback(err);
     });
   }

 // I need to return callback at the end
 // return callback();

  this.connection.end();

}

但是是的.. 它不起作用,因为我必须返回回调.. 我想我需要同步进行查询.. 我不确定。请帮忙谢谢。

【问题讨论】:

  • 为什么不使用 forEach 循环函数
  • 它是for循环上的同步版本

标签: mysql node.js


【解决方案1】:

查看我的内联 cmets:

sort: function(ids, callback) {
  this.create_connection();
  this.connection.connect();
  var q = "UPDATE mytable SET sequence CASE id ";

  // Don't execute one query per index in ids - that's inefficient
  // Instead, pack up all the queries and execute them at once
  for(var i=0; i<ids.length;i++) {
    q += "WHEN " + ids[i] + " THEN " + i + " ";
  }
  q += "ELSE sequence END;";

  // The sort method will return the result of connection.query
  return this.connection.query(q, function(err, result) {
    // End the connection
    this.connection.end();
    if(err) {
      // Handle any error here
      return callback(err);
    }
    // Otherwise, process, then return the result
    return callback(err, result);
  });
}

这里有一些更优雅的东西:

sort: function(ids, callback) {
  this.create_connection();
  this.connection.connect();

  // Don't execute one query per index in ids - that's inefficient
  // Instead, pack up all the queries and execute them at once
  var q = ids.reduce(function(pv, cv, ci){
    return pv + " WHEN " + cv + " THEN " + ci + " ";
  }, "UPDATE mytable SET sequence CASE id ") + " ELSE sequence END;";

  // The sort method will return the result of connection.query
  return this.connection.query(q, function(err, result) {
    // End the connection
    this.connection.end();
    if(err) {
      // Handle any error here
      return callback(err);
    }
    // Otherwise, process, then return the result
    return callback(err, result);
  });
}

如果你想使用 ES6 箭头函数,你可以将前面示例中的 .reduce 替换为以下内容:

var q = ids.reduce((pv, cv, ci) => pv + " WHEN " + cv + " THEN " + ci + " ",
          "UPDATE mytable SET sequence CASE id ") + " ELSE sequence END;";

【讨论】:

  • 嗨 Tex 谢谢.. 我收到解析错误 { [Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE mytable SET sequence=1 where id=1; UPDATE mytable SET seq' at line 1] code: 'ER_PARSE_ERROR', errno: 1064, sqlState: '42000', index: 0 }
  • 你是对的 - 我的 mysql 已经很生锈了。我将重新编写示例代码。为混乱道歉。
【解决方案2】:

如果您是异步世界的新手,您应该看看模块“异步”。

然后你可以这样做:

async.forEachOfSeries(ids, function(id,index,callback){
     var q = "UPDATE mytable SET sequence="+index+" where id="+id+"; ";

      this.connection.query(q, function(err, result) {         
         callback();
     });


},function done(){
    // whatever you want to do onces all the individual updates have been executed.
})

【讨论】:

  • 谢谢.. 我应该在哪里初始化和增加 i 变量??
  • 修改了示例,以便您可以拥有索引(使用 forEachOf 方法)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-05
  • 2014-09-29
相关资源
最近更新 更多