Html5 IndexedDB - 通过键检索值
在大多数教程中,都展示了如何通过使用游标迭代来从对象存储中检索所有记录。 但是如何通过键“myKey”从对象存储中检索值?
In most turorials it is shown how to retrieve all the records from an object store by iterating over them with a cursor.
But how do you retrieve a value from an object store by its key "myKey" ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您正在寻找简单的键/值存储,那么您可能需要考虑
localStorage
。它比使用 IndexedDB 简单得多。它仅适用于字符串(到目前为止),但对象可以通过 JSON 对象轻松字符串化。也就是说,只有当您需要获取一个键或一系列键的多个值时,才需要游标从 IndexedDB 中的键获取单个值。要从键获取值,您需要执行以下两种操作之一:
1) 如果
myKey
是您的主键(在创建商店时添加):然后您可以添加
onsuccess
和onerror
回调来解析生成的事件对象,您需要从中提取event.target.result
值。2) 如果
myKey
位于非主索引(您在创建商店后添加的索引)中,则打开事务上的索引,然后打开索引上的游标。至于游标(您可能会更频繁地讨论它,因为每当您在同一键下存储多个值时,您就需要一个游标),您也可以使用它们来检索单个键的值。 规范说:
您需要使用
IDBKeyRange
对象生成keyRange
。这是来自 MIT 许可的 InDB (我正在进行的 IndexedDB 包装器)的辅助方法,该方法处理在单个值(例如InDB.range.get( value )
)或键范围上打开光标。获得
keyRange
后,您可以执行以下两件事之一:1) 如果
myKey
是您的主键(在创建商店时添加),则您可以在您的交易(带有可选方向)。2) 如果
myKey
位于非主索引(您在创建商店后添加的索引)中,则打开事务上的索引,然后打开索引上的游标。If you're looking for simple key/value storage then you might want to consider
localStorage
. It's much more straightforward than using IndexedDB. It only works with strings (so far) but objects are easily stringified via the JSON object.That said, you don't need a cursor to get a single value from a key in IndexedDB, only if you need to get multiple values for a key or range of keys. To get a value from a key, you do one of two things:
1) If
myKey
is your primary key (added when creating the store):You can then add an
onsuccess
andonerror
callback to parse the resulting event object, from which you'll want to extract theevent.target.result
value.2) If
myKey
is in a non-primary index (one you've added after creating the store), you open the index on your transaction and then open the cursor on the index.As for cursors (which you likely see discussed more often because you'll need one whenever you have multiple values stored under the same key), you can use those to retrieve values for a single key as well. Says the spec:
You'll need to generate a
keyRange
using theIDBKeyRange
object. Here's the helper method for that from MIT-licensed InDB (my work-in-progress IndexedDB wrapper), which handles opening a cursor on a single value (e.g.InDB.range.get( value )
) or a key range.Once you have the
keyRange
, you do one of two things:1) If
myKey
is your primary key (added when creating the store), you open up a regular cursor on your transaction (with an optional direction).2) If
myKey
is in a non-primary index (one you've added after creating the store), you open the index on your transaction and then open the cursor on the index.有一些库可以帮助您使用 IndexedDB,例如 Dexie、JsStore、LocalForage 和 SFDatabase-js。
对我来说,使用 SFDatabase-js 更容易,因为用法几乎与 < a href="https://github.com/ScarletsFiction/Scarlets/wiki/Database" rel="nofollow noreferrer">PHP 版本(使用 MySQL/Redis 作为数据库时)。它也适用于 NodeJS。
您所需要的只是定义数据库索引结构并初始化数据库。
之后,您可以在数据库中存储几乎任何内容。
如果您想获取单行数据,可以使用
get
。并且还获取多行数据。
There are some library that can help you using IndexedDB like Dexie, JsStore, LocalForage, and SFDatabase-js.
For me, it's easier with SFDatabase-js because the usage is almost similar with the PHP version when using MySQL/Redis as database. It also working for NodeJS.
All you need is define the database indexes structure and initialize the database.
And after that, you can store almost anything in the database.
If you like to obtaining single row data you can use
get
.And also obtaining multi row data.