node.js sqlite3 - 动态添加行时出错

node.js sqlite3 - error when adding rows dynamically

当我通过 for 循环将 20 个字符串添加到我的 table 时会发生此错误:

错误:SQLITE_ERROR:无法在错误(本机)的事务中启动事务

相关代码如下:

var sqlite3 = require('sqlite3').verbose();
var Players_db = new sqlite3.Database('./db/Players');

Players_db.serialize(function() 
{
    Players_db.run("BEGIN TRANSACTION");
    Players_db.run("CREATE TABLE IF NOT EXISTS Players(string TEXT)");
    Players_db.run("COMMIT");
    console.log('Players_db init')
});

// This part is a simplification, but it shouldn't make a difference.
for (var i = 0; i < 20; i ++) 
{
    Players_db.run("BEGIN TRANSACTION");
    Players_db.run("INSERT INTO Players VALUES('"+a_string+"')");
    Players_db.run("COMMIT");
}

for 循环似乎也成功完成(并且数据正在附加到数据库),但不久之后它因上述错误而崩溃。

试试这个

        Players_db.serialize(function() 
        {
            Players_db.run("BEGIN TRANSACTION");
            for (var i = 0; i < 20; i ++) 
            {
               Players_db.run("INSERT INTO Players VALUES('"+a_string+"')");
            }
            Players_db.run("COMMIT");
        })