2

我有大量数据要将其插入 indexedDB 数据库。

数据大小约5MB。

并超过 77,000 行。

我将数据库转换为“all.js”文件,如下所示:-

const AllData = [
{ id: 1, en: "10th", ar: "arabic word" },
{ id: 2, en: "1st", ar: "arabic word" },
{ id: 3, en: "2nd", ar: "arabic word" },
{ id: 4, en: "3rd", ar: "arabic word" },
{ id: 5, en: "4th", ar: "arabic word" },
{ id: 6, en: "5th", ar: "arabic word" },
{ id: 7, en: "6th", ar: "arabic word" },
{ id: 8, en: "7th", ar: "arabic word" },
{ id: 9, en: "8th", ar: "arabic word" },

to about 77,000


];

以及我的 HTML 和 JavaScript 代码

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">

<script src="all.js" type="text/javascript"></script>


<script type="text/javascript">
/*
var custom = [
    {"id":1 ,"name":"hussein","email":"test@gmail.com"},
    {"id":2 ,"name":"ali","email":"test2@gmail.com"}

];
*/

var db;
var request = window.indexedDB.open("YesData13", 1);




request.onerror = function(event) {
  alert("Why didn't you allow my web app to use IndexedDB?!");
};


request.onsuccess = function(event) {
  db = event.target.result;





/*

var transaction = db.transaction(["data"], "readwrite");



transaction.oncomplete = function(event) {
  alert("All done!");
};


transaction.onerror = function(event) {
  // Don't forget to handle errors!
};




var objectStore = transaction.objectStore("data");




for (var i in AllData) {
  var request = objectStore.add(AllData[i]);
  request.onsuccess = function(event) {
    // event.target.result == customerData[i].ssn;
  };
}


*/


};






request.onupgradeneeded = function(event) { 
  var db = event.target.result;


  var objectStore = db.createObjectStore("data", { keyPath: "id" });
  //objectStore.createIndex("en","en",{unique:true});
  //objectStore.createIndex("ar","ar",{unique:false});


  for (var i in AllData){
    objectStore.put(AllData[i])

  }



};









/*
for (var i in AllData) {
  var request = objectStore.add(AllData[i]);
  request.onsuccess = function(event) {
    // event.target.result == customerData[i].ssn;
  };
}
*/





    function read() {
        var transaction = db.transaction(["data"]);
        var objectStore = transaction.objectStore("data");
        var request = objectStore.get(25001);
        request.onerror = function(event) {
          alert("Unable to retrieve daa from database!");
        };
        request.onsuccess = function(event) {
          // Do something with the request.result!
          if(request.result) {
            alert("id: " + request.result.id + ", English: " + request.result.en + ", arabic: " + request.result.ar);
          } else {
            alert("Kenny couldn't be found in your database!");  
          }
        };
    }




</script>
</head>


点击这里

上面的代码在 firefox 和 google chrome 中运行良好,并且插入了所有行。但是当在 firefox os 模拟器中尝试它时它不起作用,当尝试将行数减少到 25,000 时它工作正常。

我尝试将其拆分为每个文件中大约 25,000 个文件,仅添加前 25,000 个,但未添加 25,000 个之后

4

1 回答 1

1

从我自己在 Firefox 操作系统上对 indexedDB 的实验来看,模拟器似乎对您可以使用 indexedDB 存储的数据量施加了相当小的限制。我尝试编写一个脚本来将一堆随机数据添加到单个数据库中,模拟器没有给我一个错误,但它停止允许我在大约 12,000 个条目后添加数据。但是,如果我尝试使用模拟器创建一个新数据库,它会给我一个错误,提示超出配额。

但是,当我在手机上运行这一切时,它一直在继续,我怀疑模拟器不能代表实际设备,因为 Firefox 应用程序应该不会施加 indexedDB 限制。因此,如果您可以进行测试,您的应用程序应该可以在设备上运行。

于 2013-07-23T18:48:31.713 回答