SHARE
    TWEET
    userdude

    webkit/Chrome simple WebSQL demo

    Dec 27th, 2012
    338
    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. run.addEventListener('click', query);
    34. open();
    35. function query() {
    36. transact('SELECT * FROM Cities', view);
    37. }
    38. function populate(tx) {
    39. var city,
    40. i = 0;
    41. if (cities) {
    42. if (!dropped) {
    43. dropped = true;
    44. transact('DROP TABLE IF EXISTS Cities', populate);
    45. return;
    46. }
    47. if (!created) {
    48. created = true;
    49. transact('CREATE TABLE IF NOT EXISTS Cities (id unique, City)', populate);
    50. return;
    51. }
    52. while (city = cities.pop()) {
    53. transact('INSERT INTO Cities (id, City) VALUES (' + i++ + ', "' + city + '")');
    54. }
    55. }
    56. }
    57. function open() {
    58. if (!db && window.openDatabase) {
    59. db = window.openDatabase(shortName, version, displayName, maxSize);
    60. }
    61. if (cities) {
    62. db.transaction(populate);
    63. }
    64. return db;
    65. }
    66. function transact(query, callback) {
    67. var cb = callback,
    68. qel = document.createElement('p'),
    69. qid = queries.length;
    70. if (!open()) {
    71. console.log('HTML5 Database not supported.');
    72. return false;
    73. }
    74. db.transaction(transact_cb);
    75. qel.innerHTML = query + ' Query Result: <span id="q' + qid + '">Pending...</span>';
    76. qtext.appendChild(qel);
    77. queries[qid] = query;
    78. function transact_cb(tx) {
    79. tx.executeSql(query, [], transact_success, transact_error);
    80. }
    81. function transact_success(tx, result) {
    82. var rtext = document.getElementById('q' + qid);
    83. rtext.className = 'success';
    84. rtext.innerHTML = 'Success.';
    85. if (typeof cb == "function") {
    86. cb(result);
    87. } else if (cb != undefined) {
    88. eval(cb + "(result)");
    89. }
    90. }
    91. function transact_error(tx, error) {
    92. var rtext = document.getElementById('q' + qid);
    93. rtext.className = 'error';
    94. rtext.innerHTML = 'Error logged to console.';
    95. console.log(error);
    96. }
    97. }
    98. function view(result) {
    99. var thead = '<thead><tr>',
    100. tbody = '<tbody>',
    101. row,
    102. col;
    103. for (var i = 0, rows = result.rows.length; i < rows; ++i) {
    104. row = result.rows.item(i);
    105. tbody += '<tr>';
    106. for (col in row) {
    107. if (i === 0) {
    108. thead += "<th>" + col + "</th>";
    109. }
    110. tbody += '<td>' + row[col] + '</td>';
    111. }
    112. tbody += '</tr>';
    113. }
    114. thead += '</tr></thead>';
    115. tbody += '</tbody>';
    116. data.innerHTML = thead + tbody;
    117. }
    118. });
    119. </script>
    120. </head>
    121. <body>
    122. <input type="button" id="run" value='Run Query'>
    123. <div id="query"></div>
    124. <table id="table" border="1" cellspacing="1" cellpadding="5"></table>
    125. </body>
    126. </html>
    Advertisement
    Add Comment
    Please, Sign In to add comment
    Public Pastes
    We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
    Not a member of Pastebin yet?
    Sign Up, it unlocks many cool features!

    AltStyle によって変換されたページ (->オリジナル) /