HTML學習之Web儲存(五)

來源:互聯網
上載者:User

標籤: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>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.