
Untitled
By: a guest on
May 18th, 2012 | syntax:
None | size: 2.03 KB | hits: 17 | expires: Never
var OfflineDB = {
available: function(){
return window.openDatabase;
},
initDatabase: function(){
try {
var shortName = 'VeloracingOffline';
// var version = '2.0';
var displayName = 'VeloRacing Offline Data Storage';
var maxSize = 100000; // in bytes
this._db = openDatabase(shortName, '', displayName, maxSize);
this.createTables();
// this.updateEvents();
// this.selectAll();
} catch(e) {
if (e == 2) {
// Version mismatch.
console.log("Invalid database version.");
} else {
console.log("Unknown error " + e + ".");
}
return;
}
},
nullDataHandler: function(){
console.log("SQL Query Succeeded");
},
createTables: function(){
this.execSQL('CREATE TABLE IF NOT EXISTS events(id INTEGER PRIMARY KEY, name TEXT, presenter TEXT, registration_start_time DATETIME, registration_end_time DATETIME, start_time, DATETIME, end_time DATETIME);');
this.execSQL('CREATE TABLE IF NOT EXISTS races(id INTEGER PRIMARY KEY, event_id INTEGER, name TEXT, start_time DATETIME, field_limit INTEGER, prizes TEXT, distance TEXT, fee_in_cents INTEGER);');
this.execSQL('CREATE TABLE IF NOT EXISTS registrations(id INTEGER PRIMARY KEY, race_id INTEGER, name TEXT, notes TEXT, team_name TEXT, position INTEGER, type TEXT);');
},
execSQL: function(sql){
console.log(sql)
this._db.transaction(
function (transaction) {
transaction.executeSql(sql, [], this.nullDataHandler, this.errorHandler);
}
);
},
errorHandler: function(transaction, error){
console.log('sql error!!!')
if (error.code == 1){
// alert(error.message);
// DB Table already exists
} else {
// Error is a human-readable string.
console.log('Oops. Error was '+error.message+' (Code '+error.code+')');
}
return false;
}
}
OfflineDB.initDatabase();
OfflineDB.createTables();
OfflineDB.execSQL("INSERT INTO events (name) VALUES ('HELLO');");