標籤:style blog http color java io 資料 for
本機資料庫功能大大增強了Web應用對於本機存放區資料的方式和功能。Web時代真正進入了:“用戶端為重,服務端為輕的時代”。
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <script type="text/javascript"> window.localStorage.setItem("name", "張三"); alert(window.localStorage.getItem("name")); //如果資料庫存在則開啟,如果不存在則建立 //參數1.資料庫名稱2.版本3.資料庫描述4.資料庫大小。5.回呼函數 var dataBase = openDatabase("dbName", "1.0", "資料庫描述", 1024 * 1024); if (!dataBase) { alert("資料庫建立失敗!"); } else { alert("資料庫建立成功!"); dataBase.transaction(function(tx) { tx.executeSql("create table if not exists person(id unique,name text)"); tx.executeSql("create table if not exists department(id unique,name text)"); //參數 1 要執行的sql語句 2 填充sql語句中的問號 3 成功後的回呼函數 4 失敗後的回呼函數 兩個參數tx和失敗資訊 tx.executeSql("insert into person (id,name) values(?,?)",[2,"李四"], function(tx,result) { alert(result); },function(tx,error) { alert(error.message); }); tx.executeSql("insert into department(id,name) values(?,?)",[1,"財務部"]); }); } function test() { dataBase.transaction(function(tx) { tx.executeSql("select * from person",[], function(tx,result) { var array = []; //result 是SQLResultSet,類似於datatable for (var i = 0; i < result.rows.length; i++) { array[array.length] = result.rows.item(i); } alert(JSON.stringify(array)); }, function(tx, error) { alert(error.message); }); }); } </script></head> <body> <input type="button" onclick="test();" value="button" /> </body></html>