0

This error occurs when I add 20 strings to my table through a for-loop:

ERROR: SQLITE_ERROR: cannot start a transaction within a transaction at Error (native)

Here is the relevant code:

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");
}

It also appears that the for-loop finishes successfully (and data is being appended to the db), but shortly afterwards it crashes with the said error.

1 Answer 1

1

Try this

        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");
        })
Sign up to request clarification or add additional context in comments.

2 Comments

It works, but due to the nature of the application, I cannot add the rows from the same serialize event.
I figured out I could use a serialize function for every new row. Thank you1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.