Created
April 20, 2010 16:07
-
-
Save zachseifts/372688 to your computer and use it in GitHub Desktop.
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
// Connect to the database. | |
try { | |
if (!window.openDatabase) { | |
alert('This browser does not support window.openDatabase, boo!'); | |
} else { | |
var shortName = 'eightA'; | |
var version = '0.1'; | |
var displayName = 'eightA Climbs Database'; | |
var maxSize = 1024*1024; | |
var db = window.openDatabase(shortName, version, displayName, maxSize); | |
} | |
} catch (e) { | |
if (e == 2) { | |
alert('Invalid database version'); | |
} else { | |
alert('Unknown error '+e+'.'); | |
} | |
} | |
// Handlers | |
function nullDataHandler(transaction, reslts) { } | |
function errorHandler(transaction, error) { | |
var we_think_this_error_is_fatal = true; | |
if (error.code == 1) { | |
return true; | |
} | |
if (we_think_this_error_is_fatal) { | |
alert('Oops. Error was '+ error.message + '(Code '+error.code+')'); | |
return true; | |
} | |
return false; | |
} | |
function dataHandler(transaction, results) { | |
var string = "Green shirt list contains the following people:\n\n"; | |
for (var i=0; i<results.rows.length; i++) { | |
var row = results.rows.item(i); | |
string = string + row['location'] + " (ID "+row['id']+')\n'; | |
} | |
alert(string); | |
} | |
function listClimbsDataHandler(transaction, results) { | |
str = ''; | |
for (var i=0; i<results.rows.length; i++) { | |
var row = results.rows.item(i); | |
str = str + row['location']; | |
} | |
alert(str); | |
} | |
function createTables(db) { | |
db.transaction( | |
function (transaction) { | |
// query causes the transaction to intentionally fail if it exists | |
transaction.executeSql('CREATE TABLE climbs(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, location TEXT NOT NULL DEFAULT "Default Location", karma TEXT NOT NULL DEFAULT "awesome");', [], nullDataHandler, errorHandler); | |
transaction.executeSql('insert into climbs (location, karma) VALUES ("Beacon Heights", "Great");', [], nullDataHandler, errorHandler); | |
transaction.executeSql('insert into climbs (location, karma) VALUES ("The Dump", "Decent");', [], nullDataHandler, errorHandler); | |
transaction.executeSql('insert into climbs (location, karma) VALUES ("Grandmother", "Okay");', [], nullDataHandler, errorHandler); | |
transaction.executeSql('insert into climbs (location, karma) VALUES ("The Long Wall", "Pretty good");', [], nullDataHandler, errorHandler); | |
} | |
); | |
} | |
function listClimbs(db) { | |
db.transaction( | |
function (transaction) { | |
transaction.executeSql("SELECT * from climbs;", | |
[], | |
listClimbsDataHandler, | |
errorHandler); | |
} | |
) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment