Created
July 5, 2012 17:35
-
-
Save dhavaln/3055095 to your computer and use it in GitHub Desktop.
HTML5 Database Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML> | |
<html> | |
<head> | |
<title>Cordova</title> | |
<script type="text/javascript" src="jquery.js"></script> | |
<script type="text/javascript" charset="utf-8"> | |
var db = null; | |
//document.addEventListener("deviceready", onDeviceReady, false); | |
$(document).ready(function(){ | |
onDeviceReady(); | |
}); | |
function errorCB(tx, err) { | |
alert("Error processing SQL: "+err); | |
} | |
function successCB() { | |
console.log("success!"); | |
} | |
function onDeviceReady() { | |
var dBVersion = localStorage.getItem("db_version"); | |
if(dBVersion == null){ | |
window.db = initDB(); | |
//init table, set db_version to local storage | |
initTable(); | |
}else{ | |
window.db = openDatabase('beauty', dBVersion, 'test offline database', 200000); | |
} | |
} | |
function initDB(){ | |
var myDB; | |
try { | |
if (!window.openDatabase) { | |
alert('db init failed'); | |
} else { | |
var shortName = 'beauty'; | |
var version = '1.0'; | |
var displayName = 'test offline database'; | |
var maxSize = 20000; | |
myDB = openDatabase(shortName, version, displayName, maxSize); | |
console.log(myDB); | |
return myDB; | |
} | |
} catch(e) { | |
console.log(e); | |
if (e.name == "INVALID_STATE_ERR") { | |
console.log("Invalid database version."); | |
} else { | |
alert("Unknown error "+e+"."); | |
} | |
} | |
return myDB; | |
} | |
function initTable(){ | |
$.getJSON("data.json",function(data){ | |
db.transaction(function(tx){ | |
tx.executeSql('CREATE TABLE IF NOT EXISTS images (id INTEGER PRIMARY KEY, url TEXT, tag TEXT, type TEXT,status TEXT, unlocktime INTEGER, favorite INTEGER, phase INTEGER,other TEXT)'); | |
for(var i=0; i<data.length; i++){ | |
tx.executeSql('insert into images values ('+ data[i].id +',"' + data[i].url +'","' + data[i].tag +'","'+ data[i].type +'","' + data[i].status + '",' +data[i].unlocktime+','+data[i].favorite+','+data[i].phase+',"'+ data[i].other +'");'); | |
} | |
console.log("init ok!"); | |
}, errorCB, function(){localStorage.setItem("db_version","1.0");}); | |
}); | |
} | |
</script> | |
</head> | |
<body> | |
<input type="button" value="Get Image" onclick="test()" /> <br/> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment