javascript 导出和导入 IndexedDB 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7798098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 01:22:00  来源:igfitidea点击:

Exporting and importing IndexedDB data

javascripthtmlindexeddb

提问by JJJ

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.

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

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.

我想备份浏览器的配置文件目录可以做一个备份,但我也想潜在地使用不同的计算机,所以导出和导入数据库会很好。是否有一种简单的方法可以导出和导入 IndexedDB 数据库?特定于浏览器的解决方案是可以的。

回答by buley

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

纯 IndexedDB 规范中没有类似的内容,但是,您可以编写自己的方法来实现这一点。

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 addor putoperation)
  1. 打开 IndexedDB 数据库
  2. 创建对象存储
  3. 添加索引
  4. 循环遍历您的对象并一个一个地添加它们(通过addorput操作)

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 onsuccesscallback to the request object to capture the row value
  3. on each success callback push the row to a privileged array var.
  1. 在每个刻度上打开一个光标,左边界为零
  2. onsuccess向请求对象添加回调以捕获行值
  3. 在每次成功回调时,将行推送到特权数组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.

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

回答by Steven de Salas

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.

您可以在 WebSQL 中完成,在此处发布的解决方案之上编写一些 Javascript ,但是您将错过学习 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.

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

回答by RaphaelDDL

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

尝试使用jStorage,它支持大多数浏览器,除了没有 localStorage 的浏览器(如已弃用的 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.

将值保存到本地存储。键必须是字符串,否则抛出异常。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")

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.

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



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).

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

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

有了这些数据,你就$.jStorage.flush()可以一目了然了。

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).

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

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

如果您还没有数据库,只需用$.jStorage.set(key, value). :)