Skip to content

Instantly share code, notes, and snippets.

@lrieger
Created October 7, 2009 14:52
Show Gist options
  • Save lrieger/204108 to your computer and use it in GitHub Desktop.
Save lrieger/204108 to your computer and use it in GitHub Desktop.
Database = Class.create({
initialize: function(tableName) {
this.tableName = tableName;
this.db = openDatabase("Database Name", "1.0", "Display Name", 10000);
},
handleFirstTimers: function(firstTimeCallback, everyOtherTimeCallback) {
this.db.transaction(function(tx) {
tx.executeSql("CREATE TABLE IF NOT EXISTS " + this.tableName + " (flag REAL)",[], function(tx, result) {
tx.executeSql("SELECT flag FROM " + this.tableName, [], function(tx, result) {
if (result.rows.length == 0) {
tx.executeSql("INSERT INTO " + this.tableName + " (flag) VALUES (1)", [], function(tx, result) {
firstTimeCallback();
}, function(tx, error) {
Mojo.Log.error("There was an error inserting into " + this.tableName + ": " + error.message);
});
} else {
everyOtherTimeCallback();
}
}, function(tx, error) {
Mojo.Log.error("There was a problem selecting the column from " + this.tableName + " table: " + error.message);
});
}, function(tx, error) {
Mojo.Log.error("There was a problem creating the " + this.tableName + " table: " + error.message);
});
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment