使用 .csv 文件数据填充 IndexedDB
如何使用 csv 文件填充 IndexedDB?我在网上找不到任何非常简单的指南。
这是我想要使用的 csv 文件之一的示例,它们都非常类似: http ://www.mediafire.com/?hlx2tpacw5dqat5
第一列(向下第二行)是时间戳(MS Excel),第二列(向下第二行)是读数量,所有其他列是温度读数。上面的 900 表示读数之间的时间间隔(以秒为单位)。一行中的最终读数为空,应从读数数量中丢弃(或取出)。因此,行中的第一个读数是在行时间戳减去(900 秒乘以(行中的读数数减一))时获取的。对于第一行的第一个值,该值取自 (40271.0625-(0.00024*900*(1024-1)))。
可以这么说,我想要在我的 IndexedDB 中拥有两列。一个用于读数时间(显然这些是唯一的),另一个用于温度。有什么想法吗?任何帮助将不胜感激!
谢谢
How do you populate an IndexedDB with a csv file? I can't find a very simple guide anywhere online.
Here is an example of one of the csv files that I want to use, they're all pretty much like that: http://www.mediafire.com/?hlx2tpacw5dqat5
First column (row two down) is a timestamp (MS Excel), column 2 (row 2 down) is the amount of readings, all other columns are temperature readings. The 900 above indicates the time interval in seconds between readings. The final reading in a row is null and should be disgarded( or taken) away from the number of readings. Therefore, the first reading in a row is was taken at the time of the rows timestamp minus (900 seconds multiplied by the (number of readings in the row minus one)). In the case of the first value on the first row this value was taken at (40271.0625-(0.00024*900*(1024-1))).
What I'd like to have is two columns, so to speak, in my IndexedDB. One for the time of the reading (obviously these would be unique) and one for the temperatures. Any ideas? Any help would greatly be appreciated!
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用 Alasql JavaScript SQL 数据库库。它有特殊的运算符用于加载数据
TXT、CSV、TAB、XLS 和 XLSX 文件。它还可以与 IndexedDB 一起使用。在此示例中,您需要添加
将 alasql.min.js 库添加到您的项目中,然后创建或附加(如果已创建)IndexedDB 数据库,
然后只需从带有标题的 CSV 数据文件中选择列 INTO IndexedDB 表即可。
在 alasql.org 站点尝试此示例。
You can use Alasql JavaScript SQL database library. It has special operators for loading data from
TXT, CSV, TAB, XLS, and XLSX files. Also it works with IndexedDB. In this example you need to add
alasql.min.js library to your project, than create or attach (if already created) IndexedDB database,
then simply SELECT columns INTO IndexedDB table FROM CSV datafile with headers.
Try this sample at alasql.org site.
没有本地方法可以将 CSV 加载到 IndexedDB 中。但这是完全可能的,而且没有理由认为 WebSQL 是更好的选择(除了兼容性之外)^。您甚至可以使用 HTML5 File API 将 CSV 加载到浏览器中。
假设您可以使用 HTML5Rocks Todo 应用 等内容作为有关如何将行添加到 IndexedDB 数据库,困难的部分是将 CSV 文件解析为对象。 IndexedDB 使用非自然列式的“对象存储”。因此,在将 CSV 添加到数据库之前,您必须将其每一行转换为一个对象。
完成后,您必须将行一一添加到数据库中。您可能希望在要使用查找值的任何列上放置索引。在您的情况下,时间戳、读数计数和温度值。
^ WebSQL 是一个已弃用的 API,因此使用它不是一个好主意。
There's no native way to load a CSV into IndexedDB. It's totally possible though, and there's no reason why WebSQL would be a better option (aside from compatibility)^. You could even use the HTML5 File API to load the CSV into the browser.
Assuming you can use something like the HTML5Rocks Todo app as a reference on how to add rows to an IndexedDB database, hard part would be parsing the CSV file into an object. IndexedDB uses "object stores" that are not naturally columnar. So you'll have to turn each line of the CSV into an object before adding it to the database.
Once you've got that, you have to add the rows to the database one by one. You'll probably want to put an index on any columns you want to use a lookup values. In your case, the timestamp, readings count and temperate value.
^ WebSQL is a deprecated API so using it is not a good idea.
David Flanagan 在 Javascript, The Definitive Guide v6 中有一个示例,它将 csv 文件加载到索引数据库中。这对我有用。 查看 github 上的链接
David Flanagan in Javascript, The Definitive Guide v6 has an example, which loads a csv file into an indexed database. It worked for me. see link on github
假设您将 CSV 解析为数组数组,即
var data = [["时间戳", ["temp1", "temp2", "temp3"],
[“时间戳2”,[“临时4”,“临时5”,“临时6”],
ETC..];
然后我将列映射到行名称:
然后使用它们构建一个简单的字典:
这样您就可以使用
objectStore.get(rownumber)
访问行但是要更加 NoSQLish,我会将其中一列(例如“时间戳”)作为对象的键路径,并使用 multiEntry 来索引温度数组的所有子值。这样创建:
然后,当您放入时,不必费心使用密钥:
然后您可以基于此检索内容:
但真正伟大的是,现在您有了一个回到时间的温度反向索引,让您可以说一些话例如“给我温度在 20 到 30 度之间的所有记录”:
您只需小心使对象存储的关键路径指向唯一可识别的列。
Assuming you have the CSV parsed into an array-of-arrays, i.e.
var data = [["timestamp", ["temp1", "temp2", "temp3"],
["timestamp2", ["temp4", "temp5", "temp6"],
etc..];
Then I'd map the columns to row names:
and then use them to construct a simple dictionary:
This way you can access the rows with
objectStore.get(rownumber)
But to be more NoSQLish about it, I'd make one of the columns (say "timestamp") be the keypath of the object, and use multiEntry to index all the subvalues of the temperature array. Create it this way:
Then when you put, don't bother using the key:
then you can retrieve stuff based on that:
But what's really great is that now you have a reverse index of temperatures back to time, allowing you to say something like "give me all records that have temperatures between 20 and 30 degrees":
You just have to be careful about making the key path of the objectstore point to a uniquely identifiable column.
indexedDB 并不是这样工作的。您不像 SQL 数据库那样拥有表和视图等,而是拥有对象存储,其中根据键存储 JavaScript 对象。
要使对象存储模拟表,您可以使用
{row: 1, data: [col1, col2,...]}
等对象填充它。然后,您可以检索行和单元格等。但是,根据您想要如何使用数据库,这可能不是一个好的解决方案,因为表的通常约束(删除行将较低的行向上移动,没有丢失的列等)不会自动申请。也许 webSQL(基于 SQLite)会是更好的选择?indexedDB doesn't really work like that. Instead of having tables and views etc like an SQL database, you have object stores where javascript objects are stored against keys.
To make an object store emulate a table you could populate it with objects like
{row: 1, data: [col1, col2,...]}
. You can then retrieve rows and cells etc. However depending how you want to use the database this might not be a good solution since the usual constraints of a table (deleting rows shifts the lower rows up, no missing columns etc) won't automatically apply. Maybe webSQL (which is based on SQLite) would be a better option?