fork download
  1.  var database = null;                           // The client-side database
  2.  var DB_tableName = "CurrencyKeyValueTable";    // database name
  3.  
  4.  // Function: initDB() - Init and create the local database, if possible
  5.  function initDB()
  6.  {
  7.   try {
  8.    if (window.openDatabase) {
  9.     database = openDatabase("ExchangeRatesDB", "1.0",
  10.           "Exchange Rates Database", 1000);
  11.     if (database) {
  12.      database.transaction(function(tx) {
  13.       tx.executeSql("SELECT COUNT(*) FROM " + DB_tableName, [],
  14.       function(tx, result) {
  15.        loadRates();
  16.       },
  17.       function(tx, error) {
  18.        // Database doesn't exist. Let's create one.
  19.        tx.executeSql("CREATE TABLE " + DB_tableName +
  20.        " (id INTEGER PRIMARY KEY," +
  21.        "  key TEXT," +
  22.        "  value TEXT)", [], function(tx, result) {
  23.         initRates();
  24.         loadRates ();
  25.        });
  26.       });
  27.      });
  28.     }
  29.    }
  30.   } catch(e) {
  31.    database = null;
  32.   }
  33.  }
  34.  
  35.  // Function: initRates() - Initialize the default exchange rates
  36.  function initRates()
  37.  {
  38.   if (database) {
  39.    database.transaction(function (tx) {
  40.     tx.executeSql("INSERT INTO " + DB_tableName +
  41.      " (id, key, value) VALUES (?, ?, ?)", [0, 'USD', 1.44]);
  42.     tx.executeSql("INSERT INTO " + DB_tableName +
  43.      " (id, key, value) VALUES (?, ?, ?)", [1, 'EUR', 2.05]);
  44.     tx.executeSql("INSERT INTO " + DB_tableName +
  45.      " (id, key, value) VALUES (?, ?, ?)", [2, 'AUS', 1.19]);
  46.    });
  47.   }
  48.  }
  49.  
  50.  // Function: loadRates() - Load the currency exchange rates from DB
  51.  function loadRates()
  52.  {
  53.   var element;  
  54.   var popUpElement = document.getElementById('popupConvertTo');
  55.  
  56.   if (database) {
  57.    database.transaction(function(tx) {
  58.     tx.executeSql("SELECT key, value FROM " + DB_tableName, [],
  59.     function(tx, result) {
  60.      for (var i = 0; i < result.rows.length; ++i) {
  61.       var row = result.rows.item(i);
  62.       var key = row['key'];
  63.       var value = row['value'];
  64.  
  65.       //---populate the pop-up menu part---
  66.       popUpElement.options[i].text = key;
  67.       popUpElement.options[i].value = value;
  68.  
  69.       if (key == 'USD') {
  70.        element = document.getElementById('txtUSD');
  71.       }
  72.       else {
  73.        if (key == 'EUR') {
  74.         element = document.getElementById('txtEUR');
  75.        }
  76.        else if (key == 'AUS') {
  77.         element = document.getElementById('txtAUS');
  78.        }
  79.       }
  80.       element.value = value;
  81.      }
  82.     },
  83.     function(tx, error) {
  84.      showError('Failed to retrieve stored information from database - ' +
  85.       error.message);
  86.     });
  87.    });
  88.   }
  89.   else {
  90.    loadDefaultRates();
  91.   }
  92.  }
  93.  
  94.  // Function: saveRates() - Save the currency exchange rates into DB
  95.  function saveRates()
  96.  {
  97.   if (database) {
  98.    var elementUSD = document.getElementById('txtUSD');
  99.    var elementEUR = document.getElementById('txtEUR');
  100.    var elementAUS = document.getElementById('txtAUS');
  101.  
  102.    database.transaction(function (tx) {
  103.     tx.executeSql("UPDATE " + DB_tableName + " SET key = 'USD',
  104.      value = ? WHERE id = 0", [elementUSD.value]);
  105.     tx.executeSql("UPDATE " + DB_tableName + " SET key = 'EUR',
  106.      value = ? WHERE id = 1", [elementEUR.value]);
  107.     tx.executeSql("UPDATE " + DB_tableName + " SET key = 'AUS',
  108.      value = ? WHERE id = 2", [elementAUS.value]);
  109.    });
  110.   }
  111.   loadRates();
  112.  }
  113.  
  114.  // Function: deleteTable() - Delete currency exchange table from DB
  115.  function deleteTable()
  116.  {
  117.   try {
  118.    if (window.openDatabase) {
  119.     database = openDatabase("ExchangeRatesDB", "1.0",
  120.           "Exchange Rates Database");
  121.     if (database) {
  122.      database.transaction(function(tx) {
  123.       tx.executeSql("DROP TABLE " + DB_tableName, []);
  124.      });
  125.     }
  126.    }
  127.   } catch(e) {
  128.   }
  129.  }
  130.  
  131.  // Function: loadDefaultRates() - Load the default exchange rates
  132.  function loadDefaultRates()
  133.  {
  134.   var popUpElement = document.getElementById('popupConvertTo');
  135.   var element = document.getElementById('txtUSD');
  136.   element.value = "1.44";
  137.   popUpElement.options[0].text = "USD";
  138.   popUpElement.options[0].value = element.value;
  139.  
  140.   element = document.getElementById('txtEUR');
  141.   element.value = "2.05";
  142.   popUpElement.options[1].text = "EUR";
  143.   popUpElement.options[1].value = element.value;
  144.  
  145.   element = document.getElementById('txtAUS');
  146.   element.value = "1.19";
  147.   popUpElement.options[2].text = "AUS";
  148.   popUpElement.options[2].value =
  149.  }
Runtime error #stdin #stdout 0.29s 213760KB
stdin
L
stdout
Standard output is empty