我有大量数据需要将其插入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时它工作正常。
我尝试将其拆分为每个文件大约25000个文件,只添加了25,000个,但未添加25,000个
答案 0 :(得分:1)
根据我在firefox os上使用indexedDB进行的实验,似乎模拟器对indexedDB可以存储的数据量施加了相当小的限制。我尝试编写一个脚本来将一堆随机数据添加到单个数据库中,并且模拟器没有给我一个错误,但它停止了允许我在大约12,000个条目后添加数据。但是,如果我尝试使用模拟器创建一个新数据库,它会给我一个错误,说超出配额。
然而,当我在手机上运行这一切时,它只是继续前进和运行,我怀疑模拟器不能代表实际设备,因为firefox应用程序据称不会强加indexedDB限制。因此,如果您可以进行测试,那么您的应用应该可以在设备上运行。