| OLD | NEW |
| (Empty) |
| 1 var database={}; | |
| 2 database.db={}; | |
| 3 | |
| 4 database.onError = function(tx, e) { | |
| 5 var log = document.createElement('div'); | |
| 6 log.setAttribute('name','error'); | |
| 7 log.setAttribute('style','background-color:red'); | |
| 8 log.innerText = e.message; | |
| 9 document.getElementById('logs').appendChild(log); | |
| 10 } | |
| 11 | |
| 12 database.onSuccess = function(tx, r) { | |
| 13 if (r.rows.length) { | |
| 14 var ol; | |
| 15 for (var i = 0; i < r.rows.length; i++) { | |
| 16 ol = document.createElement('ol'); | |
| 17 ol.innerHTML = r.rows.item(i).ID + ": " + r.rows.item(i).docname + " (" +
r.rows.item(i).created + ")"; | |
| 18 document.getElementById('logs').appendChild(ol); | |
| 19 } | |
| 20 | |
| 21 } | |
| 22 } | |
| 23 | |
| 24 database.open=function(){ | |
| 25 database.db=openDatabase('HTML5', '1.0', 'Offline document storage', 100*1024)
; | |
| 26 } | |
| 27 | |
| 28 database.create=function(){ | |
| 29 database.db.transaction(function(tx) { | |
| 30 tx.executeSql("CREATE TABLE IF NOT EXISTS docs(ID INTEGER PRIMARY KEY ASC, d
ocname TEXT, created TIMESTAMP DEFAULT CURRENT_TIMESTAMP)", | |
| 31 [], | |
| 32 database.onSuccess, | |
| 33 database.onError); | |
| 34 });} | |
| 35 | |
| 36 database.add = function(message) { | |
| 37 database.db.transaction(function(tx){ | |
| 38 tx.executeSql("INSERT INTO docs(docname) VALUES (?)", | |
| 39 [message], database.onSuccess, database.onError); | |
| 40 }); | |
| 41 } | |
| 42 | |
| 43 database.selectAll = function() { | |
| 44 database.db.transaction(function(tx) { | |
| 45 tx.executeSql("SELECT * FROM docs", [], database.onSuccess, | |
| 46 database.onError); | |
| 47 }); | |
| 48 } | |
| 49 | |
| 50 database.onDeleteAllSuccess = function(tx, r) { | |
| 51 var doc = document.documentElement; | |
| 52 var db_completed = document.createElement("div"); | |
| 53 db_completed.setAttribute("id", "db_completed"); | |
| 54 db_completed.innerText = "db operation completed"; | |
| 55 doc.appendChild(db_completed); | |
| 56 } | |
| 57 | |
| 58 database.deleteAll = function() { | |
| 59 database.db.transaction(function(tx) { | |
| 60 tx.executeSql("delete from docs", [], database.onDeleteAllSuccess, | |
| 61 database.onError); | |
| 62 }); | |
| 63 } | |
| 64 | |
| 65 var log = document.createElement('div'); | |
| 66 log.setAttribute('name','notice'); | |
| 67 log.setAttribute('style','background-color:yellow'); | |
| 68 log.innerText = typeof window.openDatabase == "function" ? "Web Database is supp
orted." : "Web Database is not supported."; | |
| 69 document.getElementById('logs').appendChild(log); | |
| 70 | |
| 71 try { | |
| 72 database.open(); | |
| 73 database.create(); | |
| 74 database.add('Doc 1'); | |
| 75 database.add('Doc 2'); | |
| 76 database.selectAll(); | |
| 77 database.deleteAll(); | |
| 78 } catch(error) { | |
| 79 var log = document.createElement('div'); | |
| 80 log.setAttribute('name','critical'); | |
| 81 log.setAttribute('style','background-color:pink'); | |
| 82 log.innerText = error; | |
| 83 document.getElementById('logs').appendChild(log); | |
| 84 } | |
| OLD | NEW |