如何在IndexedDB中使用多个条件进行查询? [英] How do I query using multiple conditions in IndexedDB?

查看:1797
本文介绍了如何在IndexedDB中使用多个条件进行查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我发现(不幸的是)WebSQL不再支持HTML5,而IndexedDB将取代它。



我想知道是否有任何以类似于我如何使用SQL搜索满足多个条件的条目的方式来查询或搜索IndexedDB的条目。



我已经看到我可以通过KeyRange使用一个条件搜索IndexedDB。但是,我似乎无法找到任何方法来搜索两列或更多列数据,而无需从数据库中获取所有数据并使用for循环执行。



I知道这是一个在浏览器中几乎没有实现的新功能,但是我有一个项目,我正在开始研究不同的方式,我可以做到这一点。



谢谢!

解决方案

查看这个答案来回答同一个问题。这比我在这里给出的答案更详细。 store.createIndex和IDBKeyRange方法的keypath参数可以是一个数组。所以,粗略的例子:

  //在onupgradeneeded 
var store = db.createObjectStore('mystore');
store.createIndex('myindex',['prop1','prop2'],{unique:false});

//在查询部分
var transaction = db.transaction('mystore','readonly');
var store = transaction.objectStore('mystore');
var index = store.index('myindex');
//只选择那些prop1 = value1和prop2 = value2
的记录var request = index.openCursor(IDBKeyRange.only([value1,value2]));
//选择第一个匹配记录
var request = index.get(IDBKeyRange.only([value1,value2]));


I recently discovered (sadly) that WebSQL is no longer being supported for HTML5 and that IndexedDB will be replacing it instead.

I'm wondering if there is any way to query or search through the entries of an IndexedDB in a similar way to how I can use SQL to search for an entry satisfying multiple conditions.

I've seen that I can search through IndexedDB using one condition with the KeyRange. However, I can't seem to find any way to search two or more columns of data without grabbing all the data from the database and doing it with for loops.

I know this is a new feature that's barely implemented in the browsers, but I have a project that I'm starting and I'm researching the different ways I could do it.

Thank you!

解决方案

Check out this answer to the same question. It is more detailed than the answer I give here. The keypath parameter to store.createIndex and IDBKeyRange methods can be an array. So, crude example:

// 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中使用多个条件进行查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆