2013-03-26 58 views
0

這是由W3C例如,對於離線Web存儲提供的代碼:http://www.w3.org/TR/offline-webapps/HTML5離線存儲不工作

<!DOCTYPE html> 
<html> 
    <head> 
    <script type="text/javascript" charset="utf-8"> 
     function renderNote(row) { 
      console.log(row); 
     } 
     function reportError(source, message) { 
      console.log("err"); 
     } 

     function renderNotes() { 
      db.transaction(function(tx) { 
      tx.executeSql('CREATE TABLE IF NOT EXISTS Notes(title TEXT, body TEXT)', 
       []); 
      tx.executeSql('SELECT * FROM Notes', [], function(tx, rs) { 
       for(var i = 0; i < rs.rows.length; i++) { 
       renderNote(rs.rows[i]); 
       } 
      }); 
      }); 
     } 

     function insertNote(title, text) { 
      db.transaction(function(tx) { 
      tx.executeSql('INSERT INTO Notes VALUES(?, ?)', [ title, text ], 
       function(tx, rs) { 
       // … 
       }, 
       function(tx, error) { 
       reportError('sql', error.message); 
       }); 
      }); 
     } 
    </script> 
    </head> 
    <body> 
    </body> 
</html> 

沒有控制檯日誌輸出的。有什麼事?

回答

1

db的實例化和函數的執行丟失。

入住此的jsfiddle:http://jsfiddle.net/Ax5d7/4/

的JavaScript

var db = openDatabase("notes", "", "The Example Notes App!", 1048576); 

function renderNote(row) { 
    console.log(row); 
} 

function reportError(source, message) { 
    console.log("err"); 
} 

function renderNotes() { 
    db.transaction(function(tx) { 
     tx.executeSql('CREATE TABLE IF NOT EXISTS Notes(title TEXT, body TEXT)', 
     []); 

     tx.executeSql('SELECT * FROM Notes', [], function(tx, rs) { 
      for(var i = 0; i < rs.rows.length; i++) { 
       renderNote(rs.rows[i]); 
      } 
     }); 
    }); 
} 

function insertNote(title, text) { 
    db.transaction(function(tx) { 
     tx.executeSql('INSERT INTO Notes VALUES(?, ?)', [ title, text ], 
     function(tx, rs) { 
      // … 
     }, 
     function(tx, error) { 
      reportError('sql', error.message); 
     }); 
    }); 
} 

renderNotes(); 

更簡單

var db = openDatabase("notes", "", "The Example Notes App!", 10000); 

db.transaction(function(t) { 
    //t.executeSql("DROP TABLE Notes"); 
    t.executeSql("CREATE TABLE IF NOT EXISTS Notes(title TEXT, body TEXT)"); 
    t.executeSql("INSERT INTO Notes VALUES(?, ?)", [ 'title', 'content' ]); 
}); 
0

請注意http://www.w3.org/TR/webdatabase/不再被維護和支持,可以在被丟棄未來的版本。

http://www.w3.org/TR/webstorage/#storage是要走的路...

見caniuse.com對瀏覽器的支持臺。

+0

webstorage不是webSQL的替代品IndexedDB是。 localStore是一個使用非常有限的鍵值數據存儲區。現在IndexedDB比webSQL更不支持,總體來說很糟糕,試着在IndexedDB中進行左連接,你就會明白我的意思了。 – Astronaut 2013-03-27 00:49:34