Advertisement
userdude

webkit/Chrome simple WebSQL demo

Dec 27th, 2012
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  5. <title> - jsFiddle demo by userdude</title>
  6. <style type='text/css'>
  7. #query span {
  8.     font-weight: bold;
  9.     font-style: italic;
  10.     color: #888;
  11. }
  12. #query span.success {
  13.     color: green;
  14. }
  15. #query span.error {
  16.     color: red;
  17. }
  18. </style>
  19. <script type='text/javascript'>
  20. window.addEventListener('load', function load(){
  21.     var run = document.getElementById('run'),
  22.         data = document.getElementById('table'),
  23.         qtext = document.getElementById('query'),
  24.         dropped = false,
  25.         created = false,
  26.         cities = ['Houston', 'Dallas', 'Paris', 'New York', 'Buenos Aires', 'London'],
  27.         shortName = 'Cities',
  28.         version = '1.0',
  29.         displayName = 'Cities Demo',
  30.         maxSize = 5 * 1024 * 1024,
  31.         db = false,
  32.         queries = [];
  33.    
  34.     run.addEventListener('click', query);
  35.    
  36.     open();
  37.    
  38.     function query() {
  39.         transact('SELECT * FROM Cities', view);
  40.     }
  41.    
  42.     function populate(tx) {
  43.         var city,
  44.             i = 0;
  45.        
  46.         if (cities) {
  47.             if (!dropped) {
  48.                 dropped = true;
  49.                 transact('DROP TABLE IF EXISTS Cities', populate);
  50.  
  51.                 return;
  52.             }
  53.  
  54.             if (!created) {
  55.                 created = true;
  56.                 transact('CREATE TABLE IF NOT EXISTS Cities (id unique, City)', populate);
  57.  
  58.                 return;
  59.  
  60.             }
  61.            
  62.             while (city = cities.pop()) {
  63.                 transact('INSERT INTO Cities (id, City) VALUES (' + i++ + ', "' + city + '")');
  64.             }
  65.         }
  66.     }
  67.    
  68.     function open() {
  69.         if (!db && window.openDatabase) {
  70.             db = window.openDatabase(shortName, version, displayName, maxSize);
  71.         }
  72.        
  73.         if (cities) {
  74.             db.transaction(populate);
  75.         }
  76.        
  77.         return db;
  78.     }
  79.    
  80.     function transact(query, callback) {
  81.         var cb = callback,
  82.             qel = document.createElement('p'),
  83.             qid = queries.length;
  84.        
  85.         if (!open()) {
  86.             console.log('HTML5 Database not supported.');
  87.            
  88.             return false;
  89.         }
  90.        
  91.         db.transaction(transact_cb);
  92.  
  93.         qel.innerHTML = query + ' Query Result: <span id="q' + qid + '">Pending...</span>';
  94.  
  95.         qtext.appendChild(qel);
  96.  
  97.         queries[qid] = query;
  98.        
  99.         function transact_cb(tx) {
  100.             tx.executeSql(query, [], transact_success, transact_error);
  101.         }
  102.        
  103.         function transact_success(tx, result) {
  104.             var rtext = document.getElementById('q' + qid);
  105.  
  106.             rtext.className = 'success';
  107.             rtext.innerHTML = 'Success.';
  108.  
  109.             if (typeof cb == "function") {
  110.                 cb(result);
  111.             } else if (cb != undefined) {
  112.                 eval(cb + "(result)");
  113.             }
  114.         }
  115.        
  116.         function transact_error(tx, error) {
  117.             var rtext = document.getElementById('q' + qid);
  118.  
  119.             rtext.className = 'error';
  120.             rtext.innerHTML = 'Error logged to console.';
  121.  
  122.             console.log(error);
  123.         }
  124.     }
  125.    
  126.     function view(result) {
  127.         var thead = '<thead><tr>',
  128.             tbody = '<tbody>',
  129.             row,
  130.             col;
  131.  
  132.         for (var i = 0, rows = result.rows.length; i < rows; ++i) {
  133.             row = result.rows.item(i);
  134.  
  135.             tbody += '<tr>';
  136.  
  137.             for (col in row) {
  138.                 if (i === 0) {
  139.                     thead += "<th>" + col + "</th>";
  140.                 }
  141.  
  142.                 tbody += '<td>' + row[col] + '</td>';
  143.             }
  144.  
  145.             tbody += '</tr>';
  146.         }
  147.  
  148.         thead += '</tr></thead>';
  149.         tbody += '</tbody>';
  150.  
  151.         data.innerHTML = thead + tbody;
  152.     }
  153.  
  154. });
  155. </script>
  156. </head>
  157. <body>
  158. <input type="button" id="run" value='Run Query'>
  159. <div id="query"></div>
  160. <table id="table" border="1" cellspacing="1" cellpadding="5"></table>
  161. </body>
  162. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement