html5d的indexDB使用

本文通过实例演示了如何使用HTML5的IndexDB进行数据存储,包括数据库创建、数据添加及检索等关键步骤,并展示了如何利用索引和游标进行高效的数据查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

html5d的indexDB使用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <script>
        var myDB={
            name:"helloindexDB",
            version:2,
            db:null
        }
        function openDB(name,version){
            var version=version||2;
            var request=window.indexedDB.open(name,version);
            request.onerror=function(e){
                alert("发送错误");
            }
            request.onsuccess=function(e){
                myDB.db= e.target.result;
                alert("成功");
            }
            request.onupgradeneeded=function(e){
                var db= e.target.result;
                if(!db.objectStoreNames.contains("students")){
                    var store=db.createObjectStore("students",{keyPath:"id"});
                    store.createIndex("nameIndex","name",{unique:true});
                    store.createIndex("ageIndex","age",{unique:false});
                }
            }

        }
        var students=[{
            id:101,
            name:"aa",
            age:10
        }, {
            id:102,
            name:"bb",
            age:20
        },{
            id:103,
            name:"cc",
            age:11
        },{
            id:104,
            name:"dd",
            age:11
        },{
            id:105,
            name:"ff",
            age:19
        }];
        //添加数据
        function addData(db,storeName){
            var transaction=db.transaction(storeName,"readwrite");
            var store=transaction.objectStore(storeName);
            for(var i=0;i<students.length;i++){
                store.add(students[i]);
            }
        }
        openDB(myDB.name,myDB.version);
        setTimeout(function(){
            addData(myDB.db,"students")
        },1000);
        //获得数据
//        function getDataByIndexName(db,storeName){
//            var transaction=db.transaction(storeName);
//            var store=transaction.objectStore(storeName);
//            var index=store.index("nameIndex");
//            index.get("aa").onsuccess=function(e){
//                var student= e.target.result;
//                console.log(student.id+"--"+student.name+"--"+student.age);
//            }
//        }
//        setTimeout(function(){
//            getDataByIndexName(myDB.db,"students")
//        },1000);
        //游标的使用
//        function fetchStoreByCursor(db,storeName){
//            var transaction=db.transaction(storeName);
//            var store=transaction.objectStore(storeName);
//            var request=store.openCursor();
//            request.onsuccess=function(e){
//                var cursor= e.target.result;
//                if(cursor){
//                    console.log(cursor.key);
//                    var currentStudent=cursor.value;
//                    console.log(currentStudent.name);
//                    cursor.continue();
//                }
//            }
//        }
//        setTimeout(function(){
//            fetchStoreByCursor(myDB.db,"students");
//        },1000);
        //index与游标配合
        function getData(db,storeName){
            var transaction=db.transaction(storeName);
            var store=transaction.objectStore(storeName);
            var index=store.index("ageIndex");
            var request=index.openCursor(IDBKeyRange.only(11));
            request.onsuccess=function(e){
                var cursor= e.target.result;
                if(cursor){
                    var student=cursor.value;
                    document.writeln(student.id);
                    cursor.continue();
                }
            }
        }
        setTimeout(function(){
            getData(myDB.db,"students");
        },1000);
        function getDataFanwei(db,storeName){
            var transaction=db.transaction(storeName);
            var store=transaction.objectStore(storeName);
            var index=store.index("ageIndex");
//            IDBKeyRange.only(value)只获得指定数据
//            IDBKeyRange.lowerBound(value,isOpen);获得最新value,isOpen是否包含value
//            IDBKeyRange.upperBound(value,isOpen);
//            IDBKeyRange.bound(value1,value2,isOpen1,isOpen2)
            var request=index.openCursor(IDBKeyRange.only(11));
            request.onsuccess=function(e){
                var cursor= e.target.result;
                if(cursor){
                    var student=cursor.value;
                    document.writeln(student.id);
                    cursor.continue();
                }
            }
        }
        setTimeout(function(){
            getDataFanwei(myDB.db,"students");
        },1000);

    </script>
</head>
<body>

</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值