H5本地存儲-indexedDB數據庫(二)創建對象倉庫

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>創建對象倉庫</title>
    <script>
        window.indexedDB=window.indexedDB || window.webkitIndexedDB|| window.mozIndexedDB||window.msIndexedDB;
        window.IDBTransaction=window.IDBTransaction||window.webkitIDBTransaction||window.msIDBTransaction;
        window.IDBKeyRange=window.IDBKeyRange||window.webkitIDBKeyRange||window.msIDBKeyRange;
        window.IDBCursor=window.IDBCursor||window.webkitIDBCursor||window.msIDBCursor;
        function createObjectStore() {
            var dbName='indexedDBTest';
            var dbVersion=20170913;
            var idb;
            var dbConnect=indexedDB.open(dbName,dbVersion);
            dbConnect.onsuccess=function (e) {
                idb=e.target.result;
                alert("OK");
            };
            dbConnect.οnerrοr=function () {
                alert("error");
            };
            //當前版本高於上一版本才能執行這句話
            dbConnect.onupgradeneeded=function (e) {
                idb=e.target.result;
                var tx=e.target.transaction;
                var name='Users';
                //keypath是主鍵,在一個對象倉庫中只能有一個主鍵,但是主鍵是可以重複的,
                //keypath指定每一條記錄使用那一屬性值作爲主鍵
                var OptionalParameters={
                    keyPath:'userId',
                    autoIncrement:false  //是否爲自增主鍵值,如果這裏設置爲false,那麼需要顯式添加主鍵值。
                }
                //創建對象倉庫
                var store=idb.createObjectStore(name,OptionalParameters); //返回一個IDBObject對象,創建成功的對象倉庫
                alert("createObjectStore OK!")
            };


        }
    </script>
</head>
<body>
    <button οnclick="createObjectStore()">創建數據庫對象</button>
</body>
</html>

發佈了40 篇原創文章 · 獲贊 26 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章