Skip to content

Instantly share code, notes, and snippets.

@shazron
Created June 26, 2011 06:47
Show Gist options
  • Select an option

  • Save shazron/1047331 to your computer and use it in GitHub Desktop.

Select an option

Save shazron/1047331 to your computer and use it in GitHub Desktop.
Web SQLite test
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<!-- iPad/iPhone specific css below, add after your main css >
<link rel="stylesheet" media="only screen and (max-device-width: 1024px)" href="ipad.css" type="text/css" />
<link rel="stylesheet" media="only screen and (max-device-width: 480px)" href="iphone.css" type="text/css" />
-->
<!-- If you application is targeting iOS BEFORE 4.0 you MUST put json2.js from http://www.JSON.org/json2.js into your www directory and include it here -->
<script type="text/javascript" charset="utf-8" src="phonegap.0.9.5.1.min.js"></script>
<script type="text/javascript" charset="utf-8">
// If you want to prevent dragging, uncomment this section
/*
function preventBehavior(e)
{
e.preventDefault();
};
document.addEventListener("touchmove", preventBehavior, false);
*/
/* If you are supporting your own protocol, the var invokeString will contain any arguments to the app launch.
see http://iphonedevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html
for more details -jm */
/*
function handleOpenURL(url)
{
// TODO: do something with the url passed in.
}
*/
function onBodyLoad()
{
//document.addEventListener("deviceready", onDeviceReady,false);
onDeviceReady(); // not using any PhoneGap functions, so we call this directly
}
/* When this function is called, PhoneGap has been initialized and is ready to roll */
/* If you are supporting your own protocol, the var invokeString will contain any arguments to the app launch.
see http://iphonedevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html
for more details -jm */
function onDeviceReady()
{
try {
var id = 0;
var item = localStorage.getItem("id");
if (item) {
id = parseInt(item);
}
localStorage.setItem("id", ++id);
// do your thing!
var db = openDatabase('mydb', '1.0', 'my test database', 2 * 1024 * 1024);
db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS mytest (id unique, text)', [], function(tx, rs) {},
function(tx, err) { alert(JSON.stringify(err));} );
});
db.transaction(function (tx) {
tx.executeSql('INSERT INTO mytest (id, text) VALUES (?, ?)', [id, "sometext_" + id], function(tx, rs){}, function(tx, err) { alert(JSON.stringify(err)); })
});
db.transaction(function (tx) {
tx.executeSql('SELECT * FROM mytest', [],
function (tx, results) {
var len = results.rows.length, i;
var html = "<ul>"
for (i = 0; i < len; i++) {
html += "<li>";
html += results.rows.item(i).id;
html += ", ";
html += results.rows.item(i).text;
html += "</li>";
}
html += "</ul>";
document.getElementById("mydb").innerHTML = html;
});
});
} catch (e) {
alert(e);
}
}
</script>
</head>
<body onload="onBodyLoad()">
<p>Database</p>
<div id="mydb"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment