Offline access - SQLite or Indexed DB?
Asked Answered
A

8

31

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

I cannot use web storage due to quota limitations - I am comparing SQLite and Indexed DB.

  • SQLite seems to be best fit, but it is deprecated
  • Indexed DB is a good alternative, but there's no Safari support - A hybrid application is supposed to be run on the iPad and on an Android device in the future.

I am confused in the selection of API. Is there some other alternative to SQLite or support of Indexed DB expected on Safari?

Alow answered 3/9, 2012 at 11:55 Comment(2)
I know that it's a little bit late, though SQLite it's definitely not deprecated, it's just you go to this link : sqlite.org/src/timeline?t=trunk&n=1000&a=release and if you want to make it work in all actual browsers today not tomorrow it's the best option.Intaglio
@Klaus I think what Taha meant by SQLite is WebSQL which has definitely been depreceated.Prana
P
18

I think abandoning IndexedDB would be a bad idea, because it's probably the format of the future, so Safari might stop supporting 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 WebSQL I think this is probably your best solution.

Phratry answered 3/9, 2012 at 12:2 Comment(5)
Thanks for the response. Yes I am referring to WebSQL.Alow
Just be careful though. While WebSQL is not supported in some desktop browsers, it has much better support for mobile compared to IndexedDB. Check caniuse.com for more details.Meisel
Above URL talks about persistence.js - In initial googling I found that it can't be used with IndexedDB. Do you have some knowledge about that?Alow
I don't I'm afraid. But if you look further down that question there's an answer with about 20 different persistence options. You should just take a look at a few until you find one you like.Phratry
Honestly, i loathe IndexedDB with a passion. It's so horrible, and working with it is just disgusting. I started an app with it, and I'm going to switch back to WebSQL - regardless of the consequences.Sectionalism
G
19

First of all, the one that has been deprecated by W3C is WebSQL not 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

WebSQL -

  • 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

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.

If you want a quick tutorial on SQLite,

Storage of SQLite database using Android and Phonegap

Grapple answered 13/9, 2014 at 20:44 Comment(2)
If I'm not mistaken, no browser supports SQLite directly, as WebSQL is the HTML feature which uses it. If you check HTML5's storage options, you mainly have key-value, indexedDB and WebSQL (left for popularity reasons). html5test.com For any other option, you would need some plugin...Littell
As of today google search for WebSQL is returning 390K, Indexed Db - 2 million and SQLite - 13 million. In essence WebSQL is certainly coming to a dead end since it got deprecated.Ellmyer
P
18

I think abandoning IndexedDB would be a bad idea, because it's probably the format of the future, so Safari might stop supporting 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 WebSQL I think this is probably your best solution.

Phratry answered 3/9, 2012 at 12:2 Comment(5)
Thanks for the response. Yes I am referring to WebSQL.Alow
Just be careful though. While WebSQL is not supported in some desktop browsers, it has much better support for mobile compared to IndexedDB. Check caniuse.com for more details.Meisel
Above URL talks about persistence.js - In initial googling I found that it can't be used with IndexedDB. Do you have some knowledge about that?Alow
I don't I'm afraid. But if you look further down that question there's an answer with about 20 different persistence options. You should just take a look at a few until you find one you like.Phratry
Honestly, i loathe IndexedDB with a passion. It's so horrible, and working with it is just disgusting. I started an app with it, and I'm going to switch back to WebSQL - regardless of the consequences.Sectionalism
A
6

Yes, IndexedDB API is great and all browsers will support in near future.

I definitely recommend my own solution https://bitbucket.org/ytkyaw/ydn-db it is very thin wrapper for IndexedDB and fall back to Sqlite for safari.

Afrit answered 3/9, 2012 at 14:43 Comment(0)
A
3

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.com to see the current/future support in both desktop and mobile browsers.

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 storage and provide a query interface. One of the libraries, which has worked well for me is Lawnchair.

Alectryomancy answered 3/9, 2012 at 14:27 Comment(0)
F
3

If you don't want to choose between IndexedDB or WebSQL you can use the Javascript library 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 (CouchDB database) when there is an available connection.

PouchDB will depend on IndexedDB but fall back to WebSQL if IndexedDB is not supported. There is also the possibility to use a SQLite plugin for Cordova/PhoneGap.

Fletcher answered 28/4, 2015 at 14:46 Comment(0)
H
2

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.

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.

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.

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.

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!

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

Heteronomous answered 5/8, 2015 at 18:9 Comment(0)
O
1

This may be late to the game, but you could look at: 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.

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.

Nevertheless, be aware that it is a new product to the market, so that comes with both positives and negatives.

Object answered 5/10, 2012 at 17:39 Comment(3)
Thanks for the suggestion - but this option doesn't seems feasible due to storage limitation of localStorage.Alow
Great point. In the future (within a month or two), SequelSphere will support storing it's data to indexedDB, but it doesn't do that yet. As a disclaimer: I am connected to SequelSphere.Object
This site does not seem to exist anymore.Idun
B
1

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.

So to anyone who may find themselves here faced with the same decision to make, go with 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 BakedGoods if 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.

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.

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

Bridgetbridgetown answered 9/7, 2016 at 1:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.