Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 18th, 2012  |  syntax: None  |  size: 2.03 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. var OfflineDB = {
  2.   available: function(){
  3.     return window.openDatabase;
  4.   },
  5.   initDatabase: function(){
  6.     try {
  7.       var shortName   = 'VeloracingOffline';
  8.       // var version     = '2.0';
  9.       var displayName = 'VeloRacing Offline Data Storage';
  10.       var maxSize = 100000; // in bytes
  11.       this._db = openDatabase(shortName, '', displayName, maxSize);
  12.       this.createTables();
  13.       // this.updateEvents();
  14.       // this.selectAll();
  15.     } catch(e) {
  16.       if (e == 2) {
  17.         // Version mismatch.
  18.         console.log("Invalid database version.");
  19.       } else {
  20.         console.log("Unknown error " + e + ".");
  21.       }
  22.       return;
  23.     }
  24.   },
  25.  
  26.   nullDataHandler: function(){
  27.    console.log("SQL Query Succeeded");    
  28.   },
  29.  
  30.   createTables: function(){
  31.     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);');
  32.     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);');
  33.     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);');
  34.   },
  35.  
  36.   execSQL: function(sql){
  37.     console.log(sql)
  38.     this._db.transaction(
  39.       function (transaction) {
  40.         transaction.executeSql(sql, [], this.nullDataHandler, this.errorHandler);
  41.       }
  42.     );
  43.   },
  44.  
  45.   errorHandler: function(transaction, error){
  46.     console.log('sql error!!!')
  47.     if (error.code == 1){
  48.       // alert(error.message);
  49.       // DB Table already exists
  50.     } else {
  51.       // Error is a human-readable string.
  52.       console.log('Oops.  Error was '+error.message+' (Code '+error.code+')');
  53.     }
  54.     return false;
  55.   }
  56. }
  57. OfflineDB.initDatabase();
  58. OfflineDB.createTables();
  59. OfflineDB.execSQL("INSERT INTO events (name) VALUES ('HELLO');");