0

I am developing Angularjs app in vs2015. I want to open database in websql through factory function. While testing app in ripple emulator it works perfectly(also works perfectly in android OS version 4.4 and higher) but while testing app in android OS version 4.2 and lower it throws security_err dom exception 18. One more strange is that when same database, I open in simple java script file it works perfectly in ripple and in all devices also.

Here is my factory code :

angular.module('InnkeyAlert.services', ['InnkeyAlert.config'])

// DB wrapper

.factory('DB', function ($q, DB_CONFIG) {

try {
    var self = this;
    self.db = null;

    self.init = function () {
        alert("DB init start!");
        // Use self.db = window.sqlitePlugin.openDatabase({name: DB_CONFIG.name}); in production
        self.db = window.openDatabase(DB_CONFIG.name, '1.0', 'database', -1);
        alert("DB open!");
        angular.forEach(DB_CONFIG.tables, function (table) {
            var columns = [];

            angular.forEach(table.columns, function (column) {
                columns.push(column.name + ' ' + column.type);
            });

            var query = 'CREATE TABLE IF NOT EXISTS ' + table.name + ' (' + columns.join(',') + ')';
            self.query(query);
            //console.log('Table ' + table.name + ' initialized');
        });
        alert("table created !");
    };

    return self;

} catch (e) {
    //alert("Db factory: "+e.message);
}

})

Here is my index.js file code :

    var app = {

        // Application Constructor
        initialize: function () {
            this.bindEvents();
        },
        // Bind Event Listeners
        //
        // Bind any events that are required on startup. Common events are:
        // 'load', 'deviceready', 'offline', and 'online'.
        bindEvents: function () {
            alert("hi");
            document.addEventListener('deviceready', this.onDeviceReady, false);        
        },

        // The scope of 'this' is the event. In order to call the 'receivedEvent'
        // function, we must explicitly call 'app.receivedEvent(...);'
        onDeviceReady: function () {
            app.receivedEvent('deviceready');
            alert("Device ready end...");
            try {
                angular.injector(['InnkeyAlert']).get('DB').init();
                alert("inti db called...");
            } catch (e) {
                alert("index 101: " + e.message);
            }
        },
        // Update DOM on a Received Event
        receivedEvent: function (id) {

        }
    };
Rohan Kushwaha
  • 738
  • 6
  • 18

1 Answers1

0

That's a problem in Android versions lower than 4.4. It has been answered here:

You can use Web SQL from a file:// URL in WebView. The Cordova folks managed to do it. There are just three things you must do:

1) Call setDatabaseEnabled() (duh):

2) Set the database file path. Yes, this is deprecated in Android 4.4, but it is required if you want to avoid the DOM Exception 18 in pre-Kitkat:

3) Set the onExceededDatabaseQuota handler. Yes, it's deprecated in Android 4.4, blah blah blah.

Johannes Jander
  • 4,974
  • 2
  • 31
  • 46