Advertisement
tomuwhu

WebSQL

Apr 6th, 2020
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <input id="x" onchange="f(event)">
  2. <div id="list"></div>
  3. <script>
  4. var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);
  5. db.transaction( tx => {  
  6.    tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id unique, log)');
  7. } )
  8. function f( event ) {
  9.     db.transaction( tx => {
  10.         tx.executeSql(`
  11.             INSERT INTO LOGS
  12.                 (id, log)
  13.             VALUES
  14.                 ('${ new Date() }', '${ event.target.value }' )
  15.         `)
  16.         event.target.value = ''
  17.     } )
  18.     getall()
  19. }
  20. function getall() {
  21.     db.transaction( tx => {
  22.         tx.executeSql(`DELETE FROM LOGS WHERE log=''`)
  23.     } )
  24.     db.transaction( tx => {
  25.         tx.executeSql(`SELECT * FROM LOGS`, [], (tx, res) => {
  26.             document.getElementById('list').innerHTML = `<table>${
  27.                 Object.values(res.rows)
  28.                       .map( v =>
  29.                         `<tr>
  30.                             <td>${v.log}</td>
  31.                             <td><button onclick="del('${v.log}')">del</button></td>
  32.                         </tr>`)
  33.                       .join(``)
  34.             }</table>`
  35.         } )
  36.     } )
  37. }
  38. function del(s) {  
  39.     db.transaction( tx => {      
  40.         tx.executeSql(`DELETE FROM LOGS WHERE log='${s}'`, [], getall )
  41.     } )
  42. }
  43. getall()
  44. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement