我正在使用节点运行以下脚本:
var mariadb = require("mariasql");
var db = new mariadb();
db.connect({
host: "localhost",
user: "root",
password: "bus1708v2.0",
db: "test"
});
db.query("INSERT INTO Persons (LastName, FirstName) VALUES ('Some', 'Name')")
.on("result", function(result){
result.on("end", function(info){
console.log(info);
console.log(result);
});
});它成功地插入到数据库中,但我必须按ctrl-c来终止脚本。
更新
我刚刚意识到我需要把db.end()放在最后面
在本例中,db.end()是否等待所有查询完成?
发布于 2013-08-13 18:42:05
试着打
db.end();一旦您完成了对数据库的写入,因为驱动程序保持连接打开,从而阻止Node.js关闭。所以你的代码应该是
db.query("INSERT INTO Persons (LastName, FirstName) VALUES ('Some', 'Name')")
.on("result", function(result){
result.on("end", function(info){
console.log(info);
console.log(result);
db.end();
});
});或者,只需将对db.end();的调用放到示例的最后一行。根据文档,驱动程序只在所有排队命令运行后才关闭连接。
https://stackoverflow.com/questions/18216603
复制相似问题