使用多个条件查询 [英] Query using multiple conditions

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

问题描述

我最近发现(遗憾的是)HTML5不再支持WebSQL,而且IndexedDB将替换它。

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

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

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.

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

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知道这是一个新功能,几乎没有在浏览器中实现,但我有一个项目,我正在开始,我正在研究我可以做的不同方式。

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.

谢谢!

推荐答案

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

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]));

这篇关于使用多个条件查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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