节点Mysql在调用quit后无法排队查询

时间:2013-01-15 07:53:18

标签: mysql node.js express npm

我在哪里关闭mysql连接?

我需要按顺序运行查询。我正在编写目前看起来像这样的代码:

var sqlFindMobile = "select * from user_mobiles where mobile=?";
var sqlNewUser = "insert into users (password) values (?)";
//var sqlUserId = "select last_insert_id() as user_id";
var sqlNewMobile = "insert into user_mobiles (user_id, mobile) values (?,?)";
connection.connect(function(err){});
var query = connection.query(sqlFindMobile, [req.body.mobile], function(err, results) {
    if(err) throw err;
    console.log("mobile query");
    if(results.length==0) {
        var query = connection.query(sqlNewUser, [req.body.password], function(err, results) {
            if(err) throw err;
            console.log("added user");
            var user_id = results.insertId;
            var query = connection.query(sqlNewMobile, [user_id, req.body.mobile], function(err, results) {
                if(err) throw err;
                console.log("added mobile");
                    //connection.end();
                });
        });
    }
});
//connection.end();

(我是node,npm-express和npm-mysql的初学者。我试过搜索“快速mysql无法入队”来查找相关问题并且没有找到它们。)

4 个答案:

答案 0 :(得分:28)

我使用这种方法解决了这个问题。

你的connection.query函数中的

connection.end()

固定代码为here

答案 1 :(得分:10)

如果您通过 felixge 使用 node-mysql 模块,那么您可以在完成所有连接后随时调用connection.end() .query()调用,因为它会在终止连接之前等待所有查询完成。

有关详细信息,请参阅example here

如果你想要连续运行大量查询,你应该查看async module,它非常适合处理一系列异步函数(即那些有回调的函数)。

答案 2 :(得分:5)

由于Node的异步特性,可能问题是在连接已经关闭后执行mySQL查询。尝试使用此代码在线程退出之前调用connection.end():

Global conWork As ADODB.Connection
...
Set conWork = New ADODB.Connection
...
conWork.ConnectionString = "ODBC;DRIVER={SQL Server};SERVER=someServer.x.y.z;Provider=Microsoft.ACE.OLEDB.12.0;UserID=user;Password=pw;Data Source=someServer.x.y.z; Trusted_Connection=yes;"
...

conWork.Open  //...causes the error msg "OPERATION IS NOT SUPPORTED FOR THIS TYPE OF OBJECT"

改编自@Emil Condrea的代码,as of February 2015

答案 3 :(得分:1)

在我的情况下,connection.end在一个很难注意到的地方被调用,所以对connection.end的错误调用可能是这个错误的问题

相关问题