I think you've used for loop for insertion, Javascript in asynchronous in nature.
You need to execute batch sql queries in the following style not with for loop
db = window.openDatabase("Demo", "1.0", "BusinessApp", 200000);
insertIndex = 0;
var insertCounter = 0;
insertBatch = function(array, arrayLength, tableName, fieldQuestions, cb) {
console.log("Inserting Record :" + insertCounter);
if (insertIndex < arrayLength) {
db.transaction(function(tx) {
var sql = 'INSERT OR IGNORE INTO ' + tableName + ' VALUES ' + fieldQuestions;
console.log("sql: " + sql);
console.log("sql:------------- ");
console.log(array[insertIndex]);
tx.executeSql(sql, array[insertIndex],
function(tx, res) {
insertIndex++;
console.log("Insert success");
insertBatch(array, arrayLength, tableName, fieldQuestions, cb);
}, function() {
console.log("Insert failure");
});
});
} else {
insertIndex = 0;
cb();
}
}
db.transaction(populateDB, errorCB, successCB);
function populateDB(tx) {
tx.executeSql('DROP TABLE IF EXISTS ?', ["news"], function() {
console.log("success drop")
}, function() {
console.log("failure drop")
});
tx.executeSql('CREATE TABLE IF NOT EXISTS news (news_id INTEGER,news_header TEXT,news_content TEXT)');
}
function errorCB() {
alert("Error: Init.js ErrorCB");
}
function successCB() {
//alert();
console.log('DB Created Success');
var dataArray = []
dataArray.push([1, "ONE", "First News Content"])
dataArray.push([2, "TWO", "2 News Content"])
dataArray.push([3, "THREE", "2 News Content"])
dataArray.push([4, "FOUR", "4 News Content"])
insertBatch(dataArray, dataArray.length, "news", "(?,?,?)", success)
function success() {
console.log("all success")
}
}
So Every executeSQL function requires previous executeSQL success callback