//Based on http://www.html5rocks.com/en/tutorials/webdatabase/todo/ document.addEventListener("deviceready", init, false); var app = {}; app.db = null; var pageurl = ''; app.openDb = function() { alert('kk'); if (window.sqlitePlugin !== undefined) { app.db = window.sqlitePlugin.openDatabase("Todo"); alert('db'); } else { // For debugin in simulator fallback to native SQL Lite console.log("Use built in SQL Lite"); app.db = window.openDatabase("Todo", "1.0", "Cordova Demo", 200000); } } app.createTable = function() { var db = app.db; db.transaction(function(tx) { tx.executeSql("CREATE TABLE IF NOT EXISTS comm(ID INTEGER PRIMARY KEY ASC, Name TEXT,Comment TEXT,Link TEXT, added_on DATETIME)", [], [], [], []); }); } app.addTodo = function(Name, Comment, Link) { var db = app.db; db.transaction(function(tx) { var addedOn = new Date(); tx.executeSql("INSERT INTO comm(Name,Comment,Link, added_on) VALUES (?,?,?,?)", [Name,Comment,Link, addedOn], app.onSuccess, app.onError); alert('iii'); }); } app.onError = function(tx, e) { alert('apperro'); console.log("Error: " + e.message); } app.onSuccess = function(tx, r) { alert('hi'); app.refresh(pageurl); pageurl=''; } app.deleteTodo = function(id) { var db = app.db; db.transaction(function(tx) { tx.executeSql("DELETE FROM comm WHERE ID=?", [id], app.onSuccess, app.onError); }); } app.refresh = function(urlval) { pageurl = urlval; var url = pageurl; var renderTodo = function (row) { var time = row.added_on; time = time.split('GMT')[0]; return "

" + row.Comment + "

From: " + row.Name + "
Posted On:" + time + "


"; } var render = function (tx, rs) { var rowOutput = ""; var todoItems = document.getElementById("CommentdivId"); for (var i = 0; i < rs.rows.length; i++) { rowOutput += renderTodo(rs.rows.item(i)); } if (rowOutput != "") { todoItems.style.display = 'block'; todoItems.innerHTML = "

Comments

" + rowOutput + "
"; } else { todoItems.style.display = 'none'; } } var db = app.db; db.transaction(function(tx) { tx.executeSql("SELECT * FROM comm WHERE Link=?", [url], render, app.onError); }); } function init() { navigator.splashscreen.hide(); app.openDb(); app.createTable(); } function addTodo() { var todo = document.getElementById("nameid"); var comment = document.getElementById("commentid"); var url = pageurl; if (todo.value != '' && comment.value != '') { app.addTodo(todo.value, comment.value, url); } else { alert('Enter the comment'); } todo.value = ""; comment.value = ""; }