导出和导入 IndexedDB 数据

发布于 2024-12-10 06:50:11 字数 194 浏览 4 评论 0原文

我正在制作一个供我自己使用的工具,需要一个简单的数据库。这似乎是学习 HTML5 IndexedDB API 的好机会,但重要的是我在任何时候都不会丢失数据。

我想备份浏览器的配置文件目录就可以进行备份,但我也希望可能使用不同的计算机,因此导出和导入数据库会很好。有没有一种简单的方法来导出和导入 IndexedDB 数据库?特定于浏览器的解决方案是可以的。

I'm making a tool for my own use that needs a simple database. This seems like a good chance to learn the HTML5 IndexedDB API, but it's important that I don't lose data at any point.

I suppose backing up the browser's profile directory would do for a backup, but I'd also like to potentially work with different computers so exporting and importing the database would be nice. Is there an easy way to export and import IndexedDB databases? Browser-specific solutions are ok.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒废 2024-12-17 06:50:12

纯 IndexedDB 规范中没有类似的内容,但是,可以编写自己的方法来完成此任务。

导入数据的基本步骤是

  1. 打开 IndexedDB 数据库
  2. 创建对象存储
  3. 添加索引
  4. 循环遍历对象并将它们一一添加(通过 addput 操作)

要导出对象存储,您可以:

  1. 打开一个以零为左边界的游标,并在每个刻度处
  2. 向请求对象添加一个 onsuccess 回调,以捕获
  3. 每个成功回调上的行值,将行推送到特权数组var

最后一行将发出 null,您可以通过观察这种状态来确定游标何时耗尽其所有记录并完成。发生这种情况时,您可以调用导出回调,传递代表对象存储备份的特权对象数组。

There is nothing like this available in the pure IndexedDB spec, however, it's possible to write your own methods that will accomplish this.

The basic steps to import data are to

  1. open an IndexedDB database
  2. create an object store
  3. add indexes
  4. loop through your objects and add them one by one (via an add or put operation)

For exporting an object store you can:

  1. open up a cursor with zero as the left bound and at each tick
  2. add an onsuccess callback to the request object to capture the row value
  3. on each success callback push the row to a privileged array var.

The final row will emit null, which is a state you can watch for to figure out when the cursor has exhausted all its records and is done. When that happens, you can call an export callback passing the privileged array of objects representing a backup of your object store.

糖粟与秋泊 2024-12-17 06:50:12

您可以在 WebSQL 中在发布于此处,但是您将错过学习 IndexDB 的机会。

如果你真的想彻底学习IndexDB,也许你可以自己编写导入/导出工具,我认为将来会有足够的需求。

You can do it in WebSQL writing a bit of Javascript on top of a solution posted here, however you'll miss out the chance to learn IndexDB.

If you really want to learn IndexDB inside out maybe you can write the import/export tool yourself, I reckon there will be enough need for one in the future.

橘亓 2024-12-17 06:50:12

尝试使用 jStorage,它支持大多数浏览器,除了没有 localStorage 的浏览器(例如已弃用的 Safari3)

它有很多功能,但我们可以尝试用这些来实现您想要的:

set(key, value)

$.jStorage.set(key, value)

将值保存到本地存储。 key 必须是字符串,否则会抛出异常。 value 可以是任何 JSONeable 值,包括对象和数组或 XML 节点。
目前 XML 节点不能嵌套在其他对象中: $.jStorage.set("xml", xml_node) 可以,但 $.jStorage.set("xml", {xml: xml_node}) 则不行。


get(key[, default])

value = $.jStorage.get(key)
value = $.jStorage.get(key, "default value")

如果 key 存在,则 get 检索值;如果不存在,则 get 检索默认值。 key 必须是字符串,否则会抛出异常。默认值可以是任何值。


flush()

$.jStorage.flush()

清除缓存。


index()

$.jStorage.index()

以数组形式返回当前使用的所有键。

var index = $.jStorage.index();
console.log(index); // ["key1","key2","key3"]

考虑到这一点,考虑到您已经设置了数据库,您可以使用 var index = $.jStorage.index(); 并使用数组创建一个 jQuery .each() 循环,该循环获取数组的每个键并调用 get() $.jStorage.get(key) 并添加到一个大字符串,最终可以解析为 .csv,甚至 XML 或 json (你选择)。

有了这些数据在手,就可以通过$.jStorage.flush()来清除了。

然后,如果您想导入新数据库的数据,您需要做的就是使用 .each() 读取您保存的字符串/文件,并开始使用 $.jStorage 设置 kay/value par .set(键,值)

如果您还没有数据库,只需使用 $.jStorage.set(key, value) 填充一个新数据库。
:)

Try use jStorage, it supports most browsers, except the ones without localStorage (like deprecated Safari3)

It got lots of functions, but we can try achieve what you want with those:

set(key, value)

$.jStorage.set(key, value)

Saves a value to local storage. key needs to be string otherwise an exception is thrown. value can be any JSONeable value, including objects and arrays or a XML node.
Currently XML nodes can't be nested inside other objects: $.jStorage.set("xml", xml_node) is OK but $.jStorage.set("xml", {xml: xml_node}) is not.


get(key[, default])

value = $.jStorage.get(key)
value = $.jStorage.get(key, "default value")

get retrieves the value if key exists, or default if it doesn't. key needs to be string otherwise an exception is thrown. default can be any value.


flush()

$.jStorage.flush()

Clears the cache.


index()

$.jStorage.index()

Returns all the keys currently in use as an array.

var index = $.jStorage.index();
console.log(index); // ["key1","key2","key3"]

With that in mind, considering you already have a DB set up, you can use var index = $.jStorage.index(); and with the array, create a jQuery .each() loop that gets each key of the array and call the get() $.jStorage.get(key) and add to a big string, that in the end can be parsed as .csv, or even XML or json (you choose).

With these data in hands, you can $.jStorage.flush() to clear.

Then, if you want to import the data for a new DB, all you need to do is a .each() that reads the string/file you've saved and start setting the kay/value par with $.jStorage.set(key, value).

If you don't have a DB already, just populate a new one with $.jStorage.set(key, value).
:)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文