html学习笔记(14)

indexedDB数据库:

是一种存储在客户端本地的NoSql数据库;

具体参考:http://www.tfan.org/using-indexeddb/

连接数据库:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script>
        if (!window.indexedDB) {
            window.alert("Your browser doesn't support a stable version of IndexedDB. Such and such feature will not be available.")
        }
        function connectDetabase(){
            var dbName = "indexDBtest";
            var dbVersion = 1;
            var idb;
            var dbConnect = indexedDB.open(dbName, dbVersion);
            dbConnect.onsuccess = function(e){
                idb = e.target.result;
                alert("数据库连接成功");
            }
            dbConnect.onerror = function(){
                alert("连接数据库失败")
            }

        }
    </script>
</head>
<body>
    <input type="button" value="连接数据库" οnclick="connectDetabase()">
</body>
</html>


数据库版本更新:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script>
        function VersionUpdate(){
            var dbName = "indexDBtest";
            var dbVersion = 2;
            var idb;
            var dbConnect = indexedDB.open(dbName, dbVersion);
            dbConnect.onsuccess = function(e){
                idb = e.target.result;
                alert("数据库连接成功");
            }
            dbConnect.onerror = function(){
                alert("连接数据库失败")
            }

            dbConnect.onupgradeneeded = function(e){
                idb = e.target.result;
                var tx = e.target.transaction;
                var oldV = e.oldVersion;
                var newV = e.newVersion;
                alert("版本已更新" + oldV + "---->" + newV)
            }
        }
    </script>
</head>
<body>
<input type="button" value="数据库更新" οnclick="VersionUpdate()">
</body>
</html>


创建对象仓库:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script>
        if (!window.indexedDB) {
            window.alert("Your browser doesn't support a stable version of IndexedDB. Such and such feature will not be available.")
        }
        function CreateObjectStore(){
            var dbName = "indexDBtest";
            var dbVersion = 3;
            var idb;
            var dbConnect = indexedDB.open(dbName, dbVersion);
            dbConnect.onsuccess = function(e){
                idb = e.target.result;
                alert("数据库连接成功");
            }
            dbConnect.onerror = 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="创建对象仓库" οnclick="CreateObjectStore()">
</body>
</html>















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值