如何制作本地离线数据库 [英] How to make a local offline database

查看:129
本文介绍了如何制作本地离线数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用HTML,CSS和JavaScript做一个待办事项列表应用程序,我认为存储数据的最佳方式是本地数据库。我知道如何使用 localStorage sessionStorage ,并且我也知道如何使用在线 MySQL 数据库。但是,这个应用程序必须能够脱机运行,并且应该离线存储它的数据。

有没有一种方法可以用HTML和JavaScript来完成这项工作?





回应评论:


你说你知道如何使用 localStorage ...所以这似乎是问题所在?


@Lior全部我知道 localStorage 是可以将单个结果作为变量存储,而我希望存储具有不同列的行,其中包含有关该对象的不同数据。但是, localStorage 是否可以包含一个对象,如果是的话,是否可以使用通常的对象表示法?


任何实现都可能取决于用户喜欢使用的浏览器。


@paul我认为chrome会最受欢迎。




好的,我想澄清一下,我所问的确实是我怎么能使用JavaScript和HTML 做这件事,而不是有没有一种方法可以用HTML和JavaScript做到这一点?。基本上,我想要一种SQL数据库,将其内容保存在用户的计算机上,而不是在线。



解决我的问题的方法是使用WebDB或WEBSQL(我认为它被称为类似的东西)。

解决方案

我回答这个问题迟了大约3年,但考虑到没有关于当时可用选项的实际讨论,以及OP最终选择的数据库现在已被弃用 ,我想我会在这个问题上投入我的两分钱。

首先,需要考虑是否实际需要客户端数据库。更具体地说... ...


  • 您需要数据项之间的显式还是隐式关系?

  • 对上述商品进行查询的能力如何?

  • 或超过 5 MB 在太空中?



如果您对上述所有内容回答否,请使用localStorage并将自己从WebSQL和IndexedDB蜜蜂。那么,也许只是后者的头疼,因为前者已经被废弃了。



否则,IndexedDB是本地客户端数据库的唯一选择去吧,因为它是W3C标准轨道上唯一一个。



查看 BakedGoods 的商品#关于如果您想使用这些设施等等,而不必编写低级存储操作代码。例如,将数据放入客户端支持的第一个遇到的本地数据库中,就像下面这样简单:

  bakedGoods.set({
data:[{key:key1,value:val1},{key:key2,value:val2}],
storageTypes:[indexedDB ,webSQL],

//将使用等价数据库结构的默认值进行多边填充
optionsObj:{conductDisjointly:false},

完成:function byStorageTypeStoredKeysObj,byStorageTypeErrorObj){}
});

哦,为了完全透明,BakedGoods由这个人维护:)。

I'm making a to-do list application with HTML, CSS, and JavaScript, and I think the best way for me to store the data would be a local database. I know how to use localStorage and sessionStorage, and I also know how to use an online MySQL database. However, this application must be able to run offline and should store its data offline.
Is there a way I could do this with just HTML and JavaScript?


Responding to comments:

"You said you know how to use localStorage... so what seems to be the problem?"

@Lior All I know about localStorage is that you can store a single result, as a variable whereas I wish to store a row with different columns containing diffenent data about the object. However, can localStorage hold an object and if so is it referenced with the usual object notation?

Any implementation will probably depend on what browser(s) your users prefer to use.

@paul I think chrome will be most popular.


Okay, I would like to clarify that what I was asking was indeed How can I do this with JavaScript and HTML rather than Is there a way I could do this with just HTML and JavaScript?. Basically, I wanted a type of SQL database that would save its contents on the user's machine instead of online.

What solved my problem was using WebDB or WEBSQL (I think it was called something like that that).

解决方案

I'm about 3 years late in answering this, but considering that there was no actual discussion on the available options at the time, and that the database that OP ended up choosing is now deprecated, I figured i'd throw in my two cents on the matter.

First, one needs to consider whether one actually needs a client-side database. More specifically...

  • Do you need explicit or implicit relationships between your data items?
  • How about the ability to query over said items?
  • Or more than 5 MB in space?

If you answered "no" to all of the above, go with localStorage and save yourself from the headaches that are the WebSQL and IndexedDB APIs. Well, maybe just the latter headache, since the former has, as previously mentioned , been deprecated.

Otherwise, IndexedDB is the only option as far as native client-side databases go, given it is the only one that remains on the W3C standards track.

Check out BakedGoods if you want to utilize any of these facilities, and more, without having to write low-level storage operation code. With it, placing data in the first encountered native database which is supported on a client, for example, is as simple as:

bakedGoods.set({
    data: [{key: "key1", value: "val1"}, {key: "key2", value: "val2"}],
    storageTypes: ["indexedDB", "webSQL"],

    //Will be polyfilled with defaults for equivalent database structures
    optionsObj: {conductDisjointly: false},

    complete: function(byStorageTypeStoredKeysObj, byStorageTypeErrorObj){}
});

Oh, and for the sake of complete transparency, BakedGoods is maintained by this guy right here :) .

这篇关于如何制作本地离线数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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