Html 离线访问 - SQLite 还是索引数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12247380/
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
Offline access - SQLite or Indexed DB?
提问by Taha
I am in the R&D phase of developing an application, with the following key requirements:
我正处于开发应用程序的研发阶段,具有以下关键要求:
- HTML5 web application- which will also have a hybrid version
- Forms data will be stored locally, when no Internet connection
- HTML5 Web 应用程序- 也将有一个混合版本
- 没有 Internet 连接时,表单数据将存储在本地
I cannot use web storage due to quota limitations - I am comparing SQLiteand Indexed DB.
由于配额限制,我无法使用网络存储 - 我正在比较SQLite和Indexed DB。
- SQLite seems to be best fit, but it is deprecated
- Indexed DB is a good alternative, but there's no Safarisupport - A hybrid application is supposed to be run on the iPad and on an Android device in the future.
- SQLite 似乎是最合适的,但它已被弃用
- 索引数据库是一个不错的选择,但没有Safari支持 - 混合应用程序应该在未来在 iPad 和 Android 设备上运行。
I am confused in the selection of API. Is there some other alternative to SQLite or support of Indexed DB expected on Safari?
我对API的选择感到困惑。是否有其他 SQLite 替代方案或对 Safari 预期的索引数据库的支持?
采纳答案by Robin Winslow
I think abandoning IndexedDB would be a bad idea, because it's probably the format of the future, so Safari might stop supporting WebSQL.
我认为放弃 IndexedDB 是个坏主意,因为它可能是未来的格式,所以 Safari 可能会停止支持 WebSQL。
It appears there are various JavaScript solutions to bridge the gap between the two - saving in whichever is available on the user's browser: JavaScript Library to Bridge IndexedDB and WebSQLI think this is probably your best solution.
似乎有各种 JavaScript 解决方案来弥合两者之间的差距 - 保存在用户浏览器上可用的任何一个: JavaScript 库以桥接 IndexedDB 和 WebSQL我认为这可能是您最好的解决方案。
回答by KershawRocks
First of all, the one that has been deprecated by W3C is WebSQL not SQLite
首先,被W3C弃用的是WebSQL而不是SQLite
IndexedDB -
索引数据库 -
- It is incompatible with many types of mobile OS and is only compatible with certain types of versions of mobile OS
- Developers cannot use SQL with IndexedDB. They can with SQLite and WebSQL
- Most developers actively avoid using IndexedDB as much as they can
- 它与许多类型的移动操作系统不兼容,并且只兼容某些类型的移动操作系统版本
- 开发人员不能将 SQL 与 IndexedDB 一起使用。他们可以使用 SQLite 和 WebSQL
- 大多数开发人员尽可能地避免使用 IndexedDB
WebSQL -
网络SQL -
- It has been deprecated by W3C which means it is no longer maintained or developed
- It requires another plugin called Polyfill to enable mobile applications to work with popular mobile OS such as Google Android and Apple iOS
- 它已被 W3C 弃用,这意味着它不再被维护或开发
- 它需要另一个名为 Polyfill 的插件才能使移动应用程序能够与流行的移动操作系统(例如 Google Android 和 Apple iOS)配合使用
SQLite -
SQLite -
- It received an award from Google
- SQLite has its official website. IndexedDB and WebSQL do not
- On Google, SQLite returns 4.3 million results. WebSQL returns a bit less than 700K results and IndexedDB returns 282K results.
- 它获得了谷歌的奖项
- SQLite 有其官方网站。IndexedDB 和 WebSQL 没有
- 在 Google 上,SQLite 返回 430 万条结果。WebSQL 返回的结果略少于 700K,而 IndexedDB 返回的结果为 282K。
If you want a quick tutorial on SQLite,
如果你想要一个关于 SQLite 的快速教程,
回答by Kyaw Tun
Yes, IndexedDB API is great and all browsers will support in near future.
是的,IndexedDB API 很棒,在不久的将来所有浏览器都将支持。
I definitely recommend my own solution https://bitbucket.org/ytkyaw/ydn-dbit is very thin wrapper for IndexedDB and fall back to Sqlite for safari.
我绝对推荐我自己的解决方案https://bitbucket.org/ytkyaw/ydn-db它是 IndexedDB 的非常薄的包装器,并且在 safari 中回退到 Sqlite。
回答by Romin
IndexedDB is most likely the supported database of the future and it would be best to go with that instead of WebSQL. As Raymond pointed, it is best to refer to http://www.caniuse.comto see the current/future support in both desktop and mobile browsers.
IndexedDB 很可能是未来受支持的数据库,最好使用它而不是 WebSQL。正如 Raymond 所指出的,最好参考http://www.caniuse.com以查看桌面和移动浏览器中当前/未来的支持。
Depending on the current needs of your solution, you might be fine with one of the many JavaScript libraries that are available which use the local storageand provide a query interface. One of the libraries, which has worked well for me is Lawnchair.
根据您的解决方案的当前需求,您可以使用许多可用的 JavaScript 库之一,这些库使用本地存储并提供查询接口。对我来说效果很好的图书馆之一是Lawnchair。
回答by hhh
If you don't want to choose between IndexedDB or WebSQL you can use the Javascript library PouchDB.
如果您不想在 IndexedDB 或 WebSQL 之间进行选择,您可以使用 Javascript 库PouchDB。
I use it in an Android Webview to store offline data and it works pretty well. The data are stored on a local database (using IndexedDB or WebSQL) if there is no internet connection available and is synchronized with a remote database (CouchDBdatabase) when there is an available connection.
我在 Android Webview 中使用它来存储离线数据,并且效果很好。如果没有可用的互联网连接,则数据存储在本地数据库(使用 IndexedDB 或 WebSQL)上,并在有可用连接时与远程数据库(CouchDB数据库)同步。
PouchDB will depend on IndexedDB but fall back to WebSQLif IndexedDB is not supported. There is also the possibility to use a SQLite plugin for Cordova/PhoneGap.
PouchDB 将依赖于 IndexedDB,但如果不支持 IndexedDB,则回退到 WebSQL。还可以为 Cordova/PhoneGap 使用 SQLite 插件。
回答by Malavos
I wanted to make a small edit to update this question, as google is directing to us to this question if you make a research on the subject of websql, localStorage and indexedDB. The edit was rejected, so I'm posting as a answer.
我想做一个小的编辑来更新这个问题,因为如果你对 websql、localStorage 和 indexedDB 的主题进行研究,谷歌会引导我们解决这个问题。编辑被拒绝,所以我发布作为答案。
As others stated in their answers, IndexedDB lacked a bit before on support and on content on the web for it's documentation and specification.
正如其他人在他们的回答中所说的那样,IndexedDB 在其文档和规范的支持和网络内容方面之前缺乏一点。
But IndexedDB support has been vastly improved for mobile. It improved so much that the only browser who has no support or whatsoever, is Opera Mini, but it has only 0,34% of market usage.
但对移动设备的IndexedDB 支持已得到极大改进。它的改进如此之大,以至于唯一不支持或不支持的浏览器是 Opera Mini,但它只有 0.34% 的市场使用率。
As of 2015, I would suggest any developer to move to IndexedDB, as WebSQL has been deprecated and IE and Firefox stopped supporting it (that's more than 15% of market usage for these alone!) and SQLite is losing space rapidly to IndexedDB, which have now really good documentation sources, many. Some official as well!. Some IT companies are even motivating usage as well, like IBM.
截至 2015 年,我建议任何开发人员转向 IndexedDB,因为 WebSQL 已被弃用,IE 和 Firefox 停止支持它(仅这些就占市场使用量的 15% 以上!)并且 SQLite 正在迅速失去对 IndexedDB 的空间,后者现在有非常好的文档来源,很多。还有一些官方!一些 IT 公司甚至也在鼓励使用,例如IBM。
I'm going to use it, and I have not met any problems so far. Safari has added support to it, and all major browsers as well. Go for it!
我正在使用它,到目前为止我还没有遇到任何问题。Safari 增加了对它的支持,以及所有主要浏览器。去吧!
EDIT: A personnal addendum: I tried IndexedDB. I'm a Senior on my team, and IndexedDB syntax is just too messy and complex for small storage problems - I ended using localstorage to save some simple JSON data and parsing it when I need it. Way better for anyone on my team to get it (Me as well, of course!)
编辑:个人附录:我尝试了 IndexedDB。我是我团队的一名大四学生,IndexedDB 语法对于小型存储问题来说过于混乱和复杂 - 我结束了使用 localstorage 来保存一些简单的 JSON 数据并在需要时对其进行解析。让我团队中的任何人都能更好地获得它(当然我也是!)
回答by Kevin
As other have pointed out, since this question has been asked, webSQL has been deprecated, while IndexedDB implementations now exist in all of the major browser vendors.
正如其他人指出的那样,自从提出这个问题以来,webSQL已被弃用,而 IndexedDB 实现现在存在于所有主要浏览器供应商中。
So to anyone who may find themselves here faced with the same decision to make, go with IndexedDB.
因此,对于可能在这里面临同样决定的任何人,请选择 IndexedDB。
Others here have also implied, correctly, that a choice doesn't have to be made between the two types of databases. One can simply choose (or make) a library which utilizes whichever database is available on a client machine.
这里的其他人也正确地暗示,不必在两种类型的数据库之间做出选择。人们可以简单地选择(或制作)一个库,该库利用客户端计算机上可用的任何数据库。
Check out BakedGoodsif you're looking for such a library. It establishes a uniform interface that can be used to conduct storage operations in all native, and some non-native client storage facilities. It also maintains the flexibility and options afforded to the user by each.
如果您正在寻找这样的图书馆,请查看BakedGoods。它建立了一个统一的接口,可用于在所有本地和一些非本地客户端存储设施中进行存储操作。它还保持了每个人为用户提供的灵活性和选项。
With it, conducting storage operations in whichever of the database types is supported is a matter of...
有了它,在支持的任何数据库类型中进行存储操作都是一个问题......
... specifying the appropriate operation options and equivalent configs for both database types:
...为两种数据库类型指定适当的操作选项和等效配置:
//If the operation is a set(), and the referenced structures
//don't exist, they will be created automatically.
var webSQLOptionsObj = {
databaseName: "Example_DB",
databaseDisplayName: "Example DB",
databaseVersion: "",
estimatedDatabaseSize: 1024 * 1024,
tableData: {
name: "Main",
keyColumnName: "lastName",
columnDefinitions: "(lastName TEXT PRIMARY KEY, firstName TEXT)"
},
tableIndexDataArray: [name: "First_Name_Index", columnNames: "(firstName)"]
};
var indexedDBOptionsObj = {
databaseName: "Example_DB",
databaseVersion: 1,
objectStoreData: {
name: "Main",
keyPath: lastName,
autoIncrement: false
},
objectStoreIndexDataArray: [
{name: "First_Name_Index", keyPath: "firstName", unique: false, multiEntry: false}
],
};
var optionsObj = {
conductDisjointly: false,
webSQL: webSQLOptionsObj,
indexedDB: indexedDBOptionsObj
};
... and conducting the operation:
...并进行操作:
bakedGoods.set({
data: [
{value: {lastName: "Obama", firstName: "Barack"}},
{value: {lastName: "Biden", firstName: "Joe"}}
],
storageTypes: ["indexedDB", "webSQL"],
options: optionsObj,
complete: function(byStorageTypeStoredItemRangeDataObj, byStorageTypeErrorObj){}
});
Its simple interface and unmatched storage facility support comes at the cost of lack of support for some storage facility-specific configurations. For instance, it does not support the conduction of storage operations in WebSQL tables with multi-column primary keys.
其简单的界面和无与伦比的存储设施支持是以缺乏对某些存储设施特定配置的支持为代价的。例如,它不支持在具有多列主键的WebSQL 表中进行存储操作。
So if you make heavy use of those types of features, you may want to look elsewhere.
因此,如果您大量使用这些类型的功能,您可能想看看其他地方。
Oh, and for the sake of complete transparency, BakedGoods is maintained by this guy right here :) .
哦,为了完全透明,BakedGoods 由这里的这个人维护:)。
回答by John Fowler
This may be late to the game, but you could look at: SequelSphere
这可能会迟到,但你可以看看: SequelSphere
It is a 100% HTML5/JavaScript Relational Database that works cross-browser and uses local storage to persist it's data. You can use SQL to query it as well. It is it's own database engine, and doesn't rely upon the built-in (WebSQL) relational databases. As such, it will work across all browsers.
它是一个 100% HTML5/JavaScript 关系数据库,可跨浏览器工作并使用本地存储来持久化其数据。您也可以使用 SQL 来查询它。它是自己的数据库引擎,不依赖于内置 (WebSQL) 关系数据库。因此,它将适用于所有浏览器。
While it currently only supports localStorage, the idea is to support all the standards going forward. As browsers support other types of persistence, SequelSphere would take advantage of that. The positive is that you only code against SequelSphere using standard SQL, and let it handle the persistence.
虽然它目前仅支持 localStorage,但其想法是支持所有未来的标准。由于浏览器支持其他类型的持久性,SequelSphere 将利用这一点。积极的是,您只使用标准 SQL 对 SequelSphere 进行编码,并让它处理持久性。
Nevertheless, be aware that it is a new product to the market, so that comes with both positives and negatives.
尽管如此,请注意它是市场上的新产品,因此既有积极的一面,也有消极的一面。