H5本地存储-indexedDB数据库(二)创建对象仓库

来源:互联网 发布:关于人工智能的新闻 编辑:程序博客网 时间:2024/06/08 18:48
<!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.onerror=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 onclick="createObjectStore()">创建数据库对象</button></body></html>

阅读全文
1 0
原创粉丝点击