在node-mysql中插入完成之前连接关闭

时间:2011-08-21 09:22:21

标签: mysql node.js

我的代码是

var thdb = require('./module/thdb.js');
var csv = require('./module/readcsv.js');

var db = new thdb.database('root','root','localhost','TH_DB');
var CSVdata= new Array();
var csv_row=0;
csv.readFile('data.txt',",",true,function(data){

    CSVdata[csv_row]=data;
    csv_row++;

},function(count){

    for(var k=0;k<CSVdata.length;k++)
    {
        var data=CSVdata[k];

        (function(){

            var data_copy=data;
            db.checkSymbol(data_copy.Symbol,function(exist){

            if(exist)
            {
                //write log
                console.log('Nost Inserting: '+data_copy.Symbol);
            }
            else {
                //write log
                var data_copy2=data_copy;
                var db_copy=db;
                console.log('Inserting: '+data_copy2.Symbol);
                //insert


                var DataObj = {
                    Exchange_id:1,
                    Currency_id:1,
                    symbol: data_copy2.Symbol,
                    name:data_copy2.Name
                };

                db_copy.insertSecurity(DataObj);

            }//close exist else

        });//close check db

        })();
    }
    db.close();
});

我收到了错误

Not Inserting: E
Not Inserting: X    
Not Inserting: Z

Error: No database selected
    at Function._packetToUserObject (/Users/saturngod/Dropbox/th/node_modules/mysql/lib/client.js:384:7)
    at Query._handlePacket (/Users/saturngod/Dropbox/th/node_modules/mysql/lib/query.js:33:33)
    at Client._handlePacket (/Users/saturngod/Dropbox/th/node_modules/mysql/lib/client.js:309:14)
    at Parser.<anonymous> (native)
    at Parser.emit (events.js:64:17)
    at /Users/saturngod/Dropbox/th/node_modules/mysql/lib/parser.js:71:14
    at Parser.write (/Users/saturngod/Dropbox/th/node_modules/mysql/lib/parser.js:576:7)
    at Socket.<anonymous> (native)
    at Socket.emit (events.js:64:17)
    at Socket._onReadable (net.js:678:14)

如果我不关闭数据库连接,它工作正常。但是,我在循环结束时关闭了数据库连接,我总是遇到错误。

2 个答案:

答案 0 :(得分:2)

问题是javascript是异步的,所以你在那里做的大部分都不会在db.close执行之后再尝试运行。你拥有的for循环将调用checkSymbol,但所有这一切都是发送查询。它将继续循环并发送更多,然后关闭连接,然后如果响应恰好进入,连接将被关闭。

我建议安装节点异步模块,以帮助解决其中的一些问题。 https://github.com/caolan/async#iterator

我从未使用过thdb,但我会假设'insertSecurity'有第二个参数,这是一个在插入完成时调用的函数。

以下是一些示例代码。

var async = require('async');
var thdb = require('./module/thdb.js');
var csv = require('./module/readcsv.js');

var db = new thdb.database('root','root','localhost','TH_DB');

var dataFunctions = [];

function processData(data, callback) {

  db.checkSymbol(data.Symbol, function(exist){

    if(exist) {
      console.log('Nost Inserting: '+data.Symbol);
      callback();
    }
    else {
      console.log('Inserting: '+data.Symbol);

      var DataObj = {
          Exchange_id:1,
          Currency_id:1,
          symbol: data.Symbol,
          name: data.Name
      };

      db_copy.insertSecurity(DataObj, function () {
        callback();
      });
    }
  });
}    

csv.readFile('data.txt', ',', true, function(data) {
  dataFunctions.push(function(cb) {
    processData(data, cb);
  });
}, function (count) {
  async.series(dataFunctions, function() {
    console.log('Everything inserted!');

  })
});

答案 1 :(得分:1)

为什么不在功能完成后关闭?