数据库indexDB

Posted 夜未央leo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据库indexDB相关的知识,希望对你有一定的参考价值。

 1,链接数据库

      

<!DOCTYPE html>
<html>
    <head>
        <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 connectDatabase(){
                  var dbName = "indexedDBtest";
                  var dbVersion = 1;
                  var idb;
                  var dbConnect = indexedDB.open(dbName,dbVersion);   //打开数据库
                  dbConnect.onsuccess = function(e){           //是否链接成功,链接成功之后,返回信息
                      idb = e.target.result;
                      alert("数据库链接成功")
                  }
                  dbConnect.onerror = function(e){
                       alert("数据库链接失败")
                      
                  }
                
            }
        </script>
    </head>
    <body>
        <input type="button" value="链接数据库" onclick="connectDatabase()">
    </body>   
     
</html>
View Code

 2,数据库版本更新

  

<!DOCTYPE html>
<html>
    <head>
        <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 = 2;
                  var idb;
                  var dbConnect = indexedDB.open(dbName,dbVersion);
                  dbConnect.onsuccess = function(e){
                         idb = e.target.result;
                         alert("数据库链接成功");                      
                  }
                   dbConnect.error = function(){
                            alert("数据库更新失败")                   
                   }
                    dbConnect.onupgradeneeded = function(e){
                          idb = e.target.result;
                          var tx = e.target.transaction;
                          var oldVersion = e.oldVersion;
                          var newVersion = e.newVersion;
                          alert("数据库更新成功"+oldVersion+"--"+newVersion);
                        
                    }
                
            }
        </script>
    </head>
    <body>
        <input type="button" value="链接数据库" onclick="versionUpdata()">
    </body>   
     
</html>
View Code

 3,创建对象仓库

<!DOCTYPE html>
<html>
    <head>
        <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 createObjectStore(){
                  var dbName = "indexedDBtest";
                  var dbVersion = 3;
                  var idb;
                  var dbConnect = indexedDB.open(dbName,dbVersion);
                  dbConnect.onsuccess = function(e){
                         idb = e.target.result;
                         alert("数据库链接成功");                      
                  }
                   dbConnect.error = function(){
                            alert("数据库更新失败");                   
                   }
                    dbConnect.onupgradeneeded = function(e){
                          idb = e.target.result;
                          var name = "user";
                          var optionalParameters = {
                              keyPath:"userid",
                              autoIncrement:false                                                     
                          };
                          var store = idb.createObjectStore(name,optionalParameters);
                          alert("对象仓库创建成功");
                    }            
            }
        </script>
    </head>
    <body>
        <input type="button" value="创建对象仓库" onclick="createObjectStore()">
    </body>   
     
</html>
View Code

 

以上是关于数据库indexDB的主要内容,如果未能解决你的问题,请参考以下文章

indexDB出坑指南

indexDB解决过的难题

javascript 数据库版本控制IndexDB

数据库indexDB

IndexDB

indexDB操作(部分方法不太会使用)