Skip to content

Instantly share code, notes, and snippets.

@manjeshpv
Last active August 29, 2015 14:07
Show Gist options
  • Save manjeshpv/b74bba350e68c33551a4 to your computer and use it in GitHub Desktop.
Save manjeshpv/b74bba350e68c33551a4 to your computer and use it in GitHub Desktop.
webSQL Demo
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 ?', ["rmf_news"], function() {
console.log("success drop")
}, function() {
console.log("failure drop")
});
tx.executeSql('CREATE TABLE IF NOT EXISTS rmf_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, "rmf_news", "(?,?,?)", success)
function success() {
console.log("all success")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment