gpt4 book ai didi

indexing - 根据索引删除IndexedDB中的多条记录

转载 作者:行者123 更新时间:2023-12-02 17:12:19 26 4
gpt4 key购买 nike

我正在使用 IndexedDB,并且我有两个对象存储:equip(代表不同的设备,主键tagNo)和equipParts(代表设备的部件,并具有基于标签号/序列号、主键 seqNo 的索引,以及表示该部件所属设备的字段 tagNo的一部分)。

如果我删除equip中的一条记录,我想删除equipParts中带有tagNoequip的所有记录em>(就像“whereEquipParts.tagNo=equip.tagNo”)。

摘 self 的代码:

var tx = db.transaction(["equip", "equipParts"],"readwrite");
var estore = tx.objectStore("equip");
var pstore = tx.objectStore("equipParts");
var tagIndex = pstore.index("by_tagNo");
var pdestroy = tagIndex.openCursor(IDBKeyRange.only(tagno)); //opens all records bearing the selected tag number
pdestroy.onsuccess = function() {
var cursor = pdestroy.result;
if (cursor) {
if (cursor.value.tagNo == tagno) {
pstore.delete(cursor.value.seqNo); //I guess I'm wrong here
}
cursor.continue;
}
}
pdestroy.onerror = function() {
alert("Deletion attempt NG");
}
var ereq = estore.delete(tagno);
ereq.onsuccess = function(e) {
alert("Form deletion OK");
window.location = "index.html";
}
ereq.onerror = function(e) {
alert("Form deletion NG");
window.location = "index.html";
}
db.close();

问题是只删除了equip中的记录; equipParts 中的记录保留在那里。有没有办法根据非唯一索引(可以是父对象存储的主键)删除 IndexedDB 对象存储中的多条记录?

最佳答案

您必须获取主键才能删除记录。

var pdestroy = tagIndex.openKeyCursor(IDBKeyRange.only(tagno)); 
pdestroy.onsuccess = function() {
var cursor = pdestroy.result;
if (cursor) {
pstore.delete(cursor.primaryKey);
cursor.continue();
}
}

也可以,但效率不高

var pdestroy = tagIndex.openCursor(IDBKeyRange.only(tagno)); 
pdestroy.onsuccess = function() {
var cursor = pdestroy.result;
if (cursor) {
cursor.delete();
cursor.continue();
}
}

关于indexing - 根据索引删除IndexedDB中的多条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18603993/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com