在Chrome中使用本地存储作为数据库? [英] Using local storage in Chrome as a DB?

查看:1603
本文介绍了在Chrome中使用本地存储作为数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过本地存储设置/获取数据。本质上,我建立了一个提醒扩展/地理位置,允许用户保存网址,他们的地理位置和网址的标题。每次需要保存3个数据。我使用JSON.stringfy和JSON.parse来做到这一点。这里是我卡住的地方:

I'm having success setting/getting data via local storage. Essentially I'm building a reminder extension/geo-location that allows a user to save the url, their geo-location, and title of the url. 3 pieces of data needs to be saved each time. I'm using JSON.stringfy and JSON.parse to do this. Here is where I'm stuck:


  1. 如何在不写入本地存储的最后一个条目的情况下从用户保存每个条目?每次保存url时,它都不会将其作为新条目保存在本地存储中。它只是保存在最后一个条目。

  2. 随着时间的推移,用户可能会有几十个条目,但我希望能够更新数据集中的特定条目。 (例如,如果[数据集中存储的URL之一] == [目前在浏览器url $(location).attr('href');]中的URL然后执行操作。
  1. How do I save each entry from the user without writing over the last entry in local storage? Each time I save the url it doesn't save it in local storage as a new entry. It just keeps saving over the last entry.
  2. Over time, the user will probably have dozens of entries, but I want to be able to update specific entries in the dataset. (e.g. if [one of the stored urls in the dataset] == [the url that is presently in the browser url $(location).attr('href');] then do something.

我从一些非常简单的事情开始,保存并获取数据正在工作。

I started with something very simple. Saving and getting data is working.

//save my data
var mydata = { urlTitle: myTitle, myurl: currentUrl, urlLoc: geoUrl }​​​​​​​;
localStorage.setItem("mydata", JSON.stringify(mydata));

//get my data
var returnData = localStorage.getItem("mydata");
mydata = JSON.parse(returnData);

只需要一点指导。

Just need a bit of guidance.

推荐答案

localStorageDB 正是如此。

// Setup database
var lib = new localStorageDB("library", localStorage);
lib.createTable("books", ["code", "title", "author", "year", "copies"]);

// Insert rows
lib.insert("books", {code: "B001", title: "Phantoms in the brain", author: "Ramachandran", year: 1999, copies: 10});
lib.commit();

// Query rows
lib.query("books", {author: "ramachandran"});

披露:我创作了该图书馆

Disclosure: I authored the library

这篇关于在Chrome中使用本地存储作为数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆