indexDB数据库

indexDB数据库html

参考网址:https://developer.mozilla.org/zh-CN/docs/Web/API/IndexedDB_APIweb

该数据库是一种存储在客户端本笃的NoSQL数据库,他是事务性数据库系统数据库

IndexedDB里数据以对象的形式存储,每一个对象都有一个key值索引。IndexedDB里的操做都是事务性的。一种对象存储在一个objectStore里,objectStore就至关于关系数据库里的表。IndexedDB能够有不少objectStore,objectStore里能够有不少对象。每一个对象能够用key值获取。浏览器

 

 

这是在各个浏览器中都能统一运行spa

 window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || 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;
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script>
        window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || 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 versionUpdata(){
            var dbName = "indexedDBTest";//数据库库名
            var dbVersion = 1.1;//版本号
            var idb;
            var dbConnect = indexedDB.open(dbName,dbVersion);
            //dbConnect对象为一个IDBOpenDBRequest对象,表明数据库连接的请求对象
            dbConnect.onsuccess = function(e){
                idb = e.target.result;//e.target.result对象为一个IDBDBRequest对象,表明连接成功的数据对象
                console.log("e.target.result  ::"+e.target.result);
            }
            dbConnect.onerror = function(e){
                console.log("连接数据库失败");
            }
            dbConnect.onupgradeneeded = function(e){//onupgradeneeded连接数据库是发现指定的版本号大于数据库当前版本号触发的事件
                idb = e.target.result;
                var tx = e.target.transaction;//数据版本更新事物

                /*@检测版本号的
                var oldVersion = e.oldVersion;
                 var newVersion = e.newVersion;
                    console.log("更新成功,老版本" +oldVersion ,"新版本" + newVersion);*/

               /*@建立仓库
                var name = 'Users';
                var optionalParameters = {
                    keyPath : 'UserId',
                    autoIncrement : false
                }
                var store = idb.createObjectStore(name , optionalParameters);
                console.log("建立仓库成功");*/
            }
        }
    </script>
</head>

<body>
<input type="button" value="连接数据库" onclick="versionUpdata()"/>

</body>
</html>

建立indexedDB数据库中的索引debug

须要经过数据记录对象的某个属性值来建立,他只能针对被设为索引的属性值进行检索,不能针对没有被设为索引的属性值进行检索code

 

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script>
        window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || 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 CreateIndex(){
            var dbName = "indexedDBTest";//数据库库名
            var dbVersion = 1.1;//版本号
            var idb;
            var dbConnect = indexedDB.open(dbName,dbVersion);
            //dbConnect对象为一个IDBOpenDBRequest对象,表明数据库连接的请求对象
            dbConnect.onsuccess = function(e){
                idb = e.target.result;//e.target.result对象为一个IDBDBRequest对象,表明连接成功的数据对象
                console.log("e.target.result  ::"+e.target.result);
            }
            dbConnect.onerror = function(){
                console.log("连接数据库失败");
            }
            dbConnect.onupgradeneeded = function(e){//onupgradeneeded是咱们惟一能够修改数据库结构的地方,在这里面,咱们能够建立和删除对象存储空间以及构建和删除索引
                debugger;
                idb = e.target.result;
                var tx = e.target.transaction;//数据版本更新事物

                // @建立仓库
                // 建立一个对象存储空间来持有有关咱们客户的信息。
                // 咱们将使用 "UserId" 做为咱们的 key path 由于它保证是惟一的。
                var name = 'newUsers';
                var optionalParameters = {
                    keyPath : 'UserId',
                    autoIncrement : false
                }
                //对象存储空间仅调用 createObjectStore() 就能够建立
                var store = idb.createObjectStore(name , optionalParameters);
                console.log("建立仓库成功");

                /*
                 *再建立仓库成功以后,调用仓库的createIndex方法建立索引
                * @createIndex有3个参数
                * 1字符串,表明索引名
                * 2参数值表明使用数据仓库中数据记录对象的那个属性来建立索引
                * */

                 var name = 'userNameInter'; // 建立一个索引来经过 name 搜索客户。
                 var keyPath = 'useName';
                 var optionalParameters = {
                 unique:false, //可能会有重复的,所以咱们不能使用 unique 索引。unique的属性值为true,表明同一个对象仓库的两条数据记录的索引属性值(useName)不能相同
                 multiEntry:false
                 }
                 var idx = store.createIndex(name,keyPath,optionalParameters);
                 console.log("索引建立成功");
            }
        }
    </script>
</head>

<body>
<input type="button" value="建立索引" onclick="CreateIndex()"/>

</body>
</html>
相关文章
相关标签/搜索