简体   繁体   English

Node.js上的SQLite:重载数据库

[英]SQLite on Node.js: Reopeing a database

I got a Problem with connecting to a database from two different functions in node.js. 我从node.js中的两个不同函数连接到数据库时遇到问题。

At first, i connect to the SQLite DB to read some data out of it: 首先,我连接到SQLite数据库以读取其中的一些数据:

function getRbls(linie, stop) {
return new Promise(function (resolve, reject) {

    var db = openDB();

    var sql = someSQLString;

    db.all(sql, function (err, rows) {
        if (err) reject(err);
        var rbls = [];

        if (rows != null) {
            for (var i = 0; i < rows.length; i++) {
                rbls.push(rows[i].rbl)

            }
            console.log("RBLs: " + rbls);

        } else {
            rbls = false;
        }
        resolve(rbls);
    });
    db.close();

});

This code works just fine and I had it running for a while now. 这段代码运行良好,现在我已经运行了一段时间。

Now I wanted to implement a second function which always runs AFTER the first one is finished. 现在,我想实现第二个功能,该功能始终在第一个功能完成后运行。 To save some data in a different table: 要将一些数据保存在另一个表中:

function saveLastMonitorCall(rbls,userID) {
    var db = openDB();

//Has User a last call saved?
db.get(CountUserSQL,
    function (err, userCount) {
        if (err) throw err;

        var rblsString = rbls.toString();

        if (userCount > 0) {
            //User has a call in DB
            db.run(UpdateUserSQL,
                function (err) {
                    if (err) throw err;
                    console.log("User " + userID + " updated");
                }
            );

        } else {
            //User is new to DB
            db.run(InserNewUserSQL,
                function (err) {
                    if (err) throw err;
                    console.log("New User saved");
                }
            );
        }
    }
);

The odd thing is that in the second function I get the error message on the first db.get : 奇怪的是,在第二个函数中,我在第一个db.get上收到错误消息:

SQLITE_CANTOPEN: unable to open database file at Error (native)

The function openDB(); 函数openDB(); has the following code which works fine for the first function: 具有以下代码,对第一个功能运行良好:

function openDB() {
if (db) {
    return db;
} else {
    var sqlite = require('sqlite3');
    db = new sqlite.Database('./SQLite/StammdatenDB.db', function (err) {
        if (err) {
            //console.log(err);
            throw(err);
        }
        console.log("Connected to DB");

    });
    return db;
}
}

Anyone has a clue what the error could be? 任何人都知道错误可能是什么?

Thanks in advance! 提前致谢!

You store the reference to the db in the db global variable (that's what I'm getting from the first lines of openDB ), but then you call db.close() in the first function, not clearing the db reference. 您将对db的引用存储在db全局变量中(这是我从openDB的第一行获得的openDB ),但是随后在第一个函数中调用db.close() ,而不清除db引用。

Since this is SQLite and you are closing the connection immediately I'd drop the first part of the conditional in openDB and only leave what you have in the else block. 由于这是SQLite,并且您将立即关闭连接,因此我将条件openDB的第一部分放在openDB ,只保留else块中的else

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM