SQLite-直到程序退出,NodeJS语句才会刷新

时间:2019-05-20 16:59:32

标签: javascript node.js sqlite node-sqlite3

我有这个处理大量记录的sqlite代码:

let sqlite = require('sqlite3');

let dbfile = '/tmp/notes.sqlite.db'
let db = new sqlite.Database(dbfile);

db.run('CREATE TABLE if not exists mytable (UniversalId TEXT)');
fileList = ['a', 'b', 'c', 'd']
fileList.forEach((docFilename, count) => {
    db.serialize(() => {
            console.log("Doing")
            db.run("Begin transaction");
            var sql = `INSERT INTO mytable (UniversalId) values (?)`
            db.prepare(sql).run([docFilename], function (err) {
                if (err) console.error(`ERROR ${err} for uid ${universalId} ${sql}`)
                console.log("Statement run")
            }).finalize()
        db.run("commit transaction");
})
})
console.log("Closing")
db.close()

当我提交事务时,语句不执行,只有在关闭数据库并准备退出程序之后才执行。这是输出

> node index.js 
Doing
Doing
Doing
Doing
Closing
Statement run
Statement run
Statement run
Statement run

我的问题是,如果我在forEach循环中做了足够的工作,则会用完内存。如何强制SQLite在我在此处明确使用的事务边界处执行/提交每个语句?

我已经尝试过async库,就像其他SO答案建议的那样,并得到了相同的结果:

let sqlite = require('sqlite3').verbose();
var async= require('async');

let dbfile = '/tmp/notes.sqlite.db'
let db = new sqlite.Database(dbfile);

db.run('CREATE TABLE if not exists mytable (UniversalId TEXT)');
fileList = ['a', 'b', 'c', 'd']
async.each(fileList,function(docFilename) {
            console.log("Doing")
            var sql = `INSERT INTO mytable (UniversalId) values (?)`
            db.prepare(sql).run([docFilename], function (err) {
                if (err) console.error(`ERROR ${err} for uid ${universalId} ${sql}`)
                console.log("Statement run")
            }).finalize()
})
console.log("Closing")
db.close()

0 个答案:

没有答案