Jea*_*eri 17 javascript web-sql indexeddb
我想从WebSql更改为Indexeddb.但是,如何进行SQL查询
SELECT * FROM customers WHERE ssn = '444-44-4444' and emal = 'bill@bill@company.com'
SELECT * FROM customers WHERE ssn = '444-44-4444' and emal = 'bill@bill@company.com' and age = 30
SELECT * FROM customers WHERE ssn = '444-44-4444' and emal = 'bill@bill@company.com' and name = 'Bill'
etc
Run Code Online (Sandbox Code Playgroud)
使用IndexedDB?例如,我在阅读indexedDb 的文档时注意到,所有示例当时只查询一个索引.所以你可以做到
var index = objectStore.index("ssn");
index.get("444-44-4444").onsuccess = function(event) {
alert("Name is " + event.target.result.name);
};
Run Code Online (Sandbox Code Playgroud)
但我需要同时查询多个索引!
我还发现了一些关于复合索引的有趣帖子,但它们只有在查询复合索引中的所有字段时才有效.
Kya*_*Tun 18
对于您的示例,复合索引仍然有效,但需要两个复合索引
objectStore.createIndex('ssn, email, age', ['ssn', 'email', 'age']); // corrected
objectStore.createIndex('ssn, email, name', ['ssn', 'email', 'name'])
Run Code Online (Sandbox Code Playgroud)
并查询这样的
keyRange = IDBKeyRange.bound(
['444-44-4444', 'bill@bill@company.com'],
['444-44-4444', 'bill@bill@company.com', ''])
objectStore.index('ssn, email, age').get(keyRange)
objectStore.index('ssn, email, age').get(['444-44-4444', 'bill@bill@company.com', 30])
objectStore.index('ssn, email, name').get(['444-44-4444', 'bill@bill@company.com', 'Bill'])
Run Code Online (Sandbox Code Playgroud)
索引可以按任何顺序排列,但如果最具体的话,它最有效.
或者,您也可以使用键加入.密钥连接需要四个(单个)索引.四个索引占用的存储空间更少,更通用.例如,以下查询需要另一个复合索引
SELECT * FROM customers WHERE ssn = '444-44-4444' and name = 'Bill' and age = 30
Run Code Online (Sandbox Code Playgroud)
密钥加入仍然适用于该查询.
| 归档时间: |
|
| 查看次数: |
12516 次 |
| 最近记录: |