使用nodejs和sqlite

时间:2015-12-10 20:09:22

标签: node.js sqlite

我正在寻找一些关于如何编写这段代码的最佳方法的见解。

我希望所有这些命令都能同步运行,所以我尝试使用db.serialize函数。

我需要根据查询结果做一些其他数据库的东西(顺便说一句,我还是节点新手)

我第一次尝试了这个

var db = new sqlite3.Database(file);
var stmt = "SELECT image_id FROM image WHERE file_downloaded = 1 ORDER BY image_id DESC LIMIT 1";
db.serialize(function() {
    db.all(stmt, function(err, rows) {
        if (err){
            if (err) { logger.error('Error %j',  err); throw err; }
        }
        if ( rows.length > 0 ){
                db.run("DELETE FROM image_status");
                db.run("INSERT INTO image_status ( next_new_id, next_type , restart_new  ) VALUES ("+rows[0].image_id+",'old',"+restart_new+")");
                db.run("UPDATE image_status SET next_old_id = (SELECT image_id FROM image WHERE file_downloaded = 1 ORDER BY image_id ASC LIMIT 1)");
                db.all("SELECT next_old_id FROM image_status LIMIT 1", function(err, rows) {
                    if (err) { logger.error('connection %j',  err); throw err; }
                    if ( rows.length > 0 ){
                        next_old_id = rows[0].next_old_id;
                    }
                    mycallback(next_new_id, next_old_id,'old');
                })
        }
    })
});
db.close();

但是当我到达DELETE部分时,数据库已经关闭了 所以接下来我尝试将db.serialize移动到查询的回调中,然后在回调中管理关闭数据库。这似乎不是最好的解决方案(我得到偶尔的DB忙于其他事件)。我正在寻找正确的方法。

感谢您的帮助

var db = new sqlite3.Database(file);
var stmt = "SELECT image_id FROM image WHERE file_downloaded = 1 ORDER BY image_id DESC LIMIT 1";
db.all(stmt, function(err, rows) {
    if (err){
        db.close();
        if (err) { logger.error('Error %j',  err); throw err; }
    }
    if ( rows.length > 0 ){
        db.serialize(function() {
            db.run("DELETE FROM image_status");
            db.run("INSERT INTO image_status ( next_new_id, next_type , restart_new  ) VALUES ("+rows[0].image_id+",'old',"+restart_new+")");
            db.run("UPDATE image_status SET next_old_id = (SELECT image_id FROM image WHERE file_downloaded = 1 ORDER BY image_id ASC LIMIT 1)");
            db.all("SELECT next_old_id FROM image_status LIMIT 1", function(err, rows) {
                if (err) { logger.error('connection %j',  err); throw err; }
                if ( rows.length > 0 ){
                    next_old_id = rows[0].next_old_id;
                }
                mycallback(next_new_id, next_old_id,'old');
            })
            db.close();
        });
    }else{
        db.close();
    }
})

2 个答案:

答案 0 :(得分:1)

如果您不打算实现promise,那么这是处理关闭数据库的最佳方法。

Bluebird promises有一个.finally选项,您可以放置​​db.close(),它将在完成所有db调用promise之后运行。

答案 1 :(得分:0)

重构到下面。给我带来问题的另一部分是db.all。重构后,db.all没有看到插入。更改db.each修复了该部分:

db.serialize(function() {
    db.run("DELETE FROM image_status");
    var stmt = db.prepare("INSERT INTO image_status ( restart_new , next_type ,  next_new_id,  next_old_id ) "
                          +"VALUES (?,'old'," +
                            "(SELECT image_id FROM image WHERE file_downloaded = 1 ORDER BY image_id DESC LIMIT 1)," +
                            "(SELECT image_id FROM image WHERE file_downloaded = 1 ORDER BY image_id ASC LIMIT 1))"
                          );
    stmt.run(restart_new);
    stmt.finalize();

    db.each("SELECT next_new_id, next_old_id FROM image_status LIMIT 1", function(err, row) {
        if (err) { logger.error('connection %j',  err); throw err; }
        mycallback(row.next_new_id, row.next_old_id,'old');
    });
});
db.close();