difference between window.openDatabase() and window.sqlitePlugin.openDatabase() functions?

frank picture frank · Jun 27, 2014 · Viewed 13k times · Source

Using Cordova version 3.x and Android version 2.x to 4.x.

I am wondering:

  1. Is my understanding correct that all android devices by default have an sqlite program/interface for creating sqlite database?
  2. Do both the above database function calls create a sqlite database in the device?
  3. If the above answer is no, then what type of Database do both the above function calls create?
  4. If the answer is yes, then is window.sqlite.openDatabase() function wrapper around window.openDatabase()?
  5. Are the databases created by the call persistent? That is, is the data available after closing and reopening the cordova packaged apps?
  6. Is there a maximum the database size that can be created by the above two methods?

Answer

Mister Smith picture Mister Smith · Jun 27, 2014
  1. WebSQL (window.openDatabase) is a deprecated web standard. But it is available in most desktop and mobile browsers. Most browsers implement the specification using SQLite. In Android, browsers and WebViews support WebSQL, along with local storage and session storage from the first versions, and also IndexedDB since KitKat.

Then we have that Android independently supports SQLite to be used from Java APIs as one of its main persistence mechanisms.

Cordova is special. The app runs in a WebView so it should be using WebSQL, but in Android the plugin overrides the API and implants into the window object new functions that might default to a different implementation, rather than the browser API.

So in a Cordova app, once it is loaded, when you call openDatabase you are actually calling a new function that Cordova has placed in the windows object overriding the old standard one. From the Cordova docs:

Some devices already provide an implementation of this spec. For those devices, the built-in support is used instead of replacing it with Cordova's implementation. For devices that don't have storage support, Cordova's implementation should be compatible with the W3C specification.

That quote is ambiguous and no longer in the docs. For "built-in" they meant built-in WebSQL support in the WebView. The docs I linked are old, from a 2.x version. In those versions, Cordova only defaulted to the custom implementation if the WebView did not support WebSQL (I think this never happened) or if the device was affected by the bug 16175. The default implementation consisted on using the Storage.java plugin that used the Java API to create an SQLite database. I've been reading the most recent sources and on newer (3.x) versions they seem to be using WebSQLite always.

  1. Yes both create a DB file but the path to it will be different. In fact you can open the same DB from the JavaScript code and the Java code in your app.

  2. Same type of DB. SQLite is a native C layer that manages the file structure. In fact, you could also use this native C API from a native Android app.

  3. Cordova / Phonegap use SQLIte from the built-in support if available (in Android it is).

  4. Yes, they stay there.

  5. Yes, there's a limit. Check here for more info.