我最近发现(遗憾的是)HTML5不再支持WebSQL,而且IndexedDB将替代它.
我想知道是否有任何方法可以查询或搜索IndexedDB的条目,方式与我如何使用SQL搜索满足多个条件的条目类似.
我已经看到我可以使用KeyRange的一个条件搜索IndexedDB.但是,我似乎无法找到任何方法来搜索两列或更多列数据,而无需从数据库中获取所有数据并使用for循环执行此操作.
我知道这是一个新功能,几乎没有在浏览器中实现,但我有一个项目,我正在开始,我正在研究我可以做的不同方式.
谢谢!
解决方法:
查看this answer到同一个问题.它比我在这里给出的答案更详细. store.createIndex和IDBKeyRange方法can be an array的keypath参数.所以,粗略的例子:
// In onupgradeneeded
var store = db.createObjectStore('mystore');
store.createIndex('myindex', ['prop1','prop2'], {unique:false});
// In your query section
var transaction = db.transaction('mystore','readonly');
var store = transaction.objectStore('mystore');
var index = store.index('myindex');
// Select only those records where prop1=value1 and prop2=value2
var request = index.openCursor(IDBKeyRange.only([value1, value2]));
// Select the first matching record
var request = index.get(IDBKeyRange.only([value1, value2]));
标签:indexeddb,javascript
来源: https://codeday.me/bug/20191004/1852414.html