Web SQL Database + Javascript loop

Do it the other way around:

<script>
    numberofArticles = 5;
    db = openDatabase("websql", "0.1", "web-sql testing", 10000);
    db.transaction(function(tx) {
        tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id unique, articleID int)');
    });
    db.transaction(function (tx) {  
        for (var i=0; i<=numberofArticles-1; i++){  
            tx.executeSql('INSERT INTO LOGS (articleID) VALUES (?)',  [i]);
        };
    });
</script>

And the alternative, the proper way with the loop outside which is unnecessary in this case

    for (var i=0; i<=numberofArticles-1; i++){  
      (function(i) {
        db.transaction(function (tx) {  
                tx.executeSql('INSERT INTO LOGS (articleID) VALUES (?)',  [i]);
        });
      })(i);
    };

Leave a Comment