Search code examples
androidcordovaionic-frameworkcordova-plugins

Cordova-sqlitePlugin Failed to Open Database


I'm currently doing a hybrid app using Ionic Framework.

Everything was working fine last week and WITHOUT modifying anything from the plugin it just stopped working, I haven't found any thread (just one) with any solutions or good enough approaches.

I'm running on Android and iOS devices.

ADB Logcat:

I/chromium( 1844): [INFO:CONSOLE(175)] "OPEN database: my.db", source: file:///android_asset/www/plugins/cordova-sqlite-storage/www/SQLitePlugin.js (175)
I/chromium( 1844): [INFO:CONSOLE(106)] "new transaction is waiting for open operation", source: file:///android_asset/www/plugins/cordova-sqlite-storage/www/SQLitePlugin.js (106)
I/chromium( 1844): [INFO:CONSOLE(197)] "OPEN database: my.db FAILED, aborting any pending transactions", source: file:///android_asset/www/plugins/cordova-sqlite-storage/www/SQLitePlugin.js (197)
I/chromium( 1844): [INFO:CONSOLE(83)] "Could not open database", source: file:///android_asset/www/plugins/cordova-sqlite-storage/www/SQLitePlugin.js (83)

XCode Console Log does not show much (at all).

Cordova Installed Plugins:

com.googlemaps.ios 1.13.0 "Google Map iOS SDK for Cordova"
cordova-plugin-compat 1.0.0 "Compat"
cordova-plugin-console 1.0.4 "Console"
cordova-plugin-dbcopy 1.0.4 "sqlDB"
cordova-plugin-device 1.1.2 "Device"
cordova-plugin-file 4.3.0 "File"
cordova-plugin-geolocation 2.4.0 "Geolocation"
cordova-plugin-googlemaps 1.3.9 "phonegap-googlemaps-plugin"
cordova-plugin-splashscreen 3.2.2 "Splashscreen"
cordova-plugin-sqlite-2 1.0.4 "SQLitePlugin"
cordova-plugin-statusbar 2.1.3 "StatusBar"
cordova-plugin-whitelist 1.2.2 "Whitelist"
cordova-sqlite-storage 1.4.8 "Cordova sqlite storage plugin"
ionic-plugin-keyboard 2.2.1 "Keyboard"

Implementation:

angular.module('starter.controllers', [])
.controller('RoutesCtrl', function($scope, $ionicPlatform, $http) {
    // Database functions…
    $ionicPlatform.copyDatabaseFile = function(db_name)
    {

        var sourceFileName = cordova.file.applicationDirectory + 'www/' + db_name;
        var targetDirName = cordova.file.dataDirectory;

        return Promise.all([
            new Promise(function (resolve, reject) {
                resolveLocalFileSystemURL(sourceFileName, resolve, reject);
            }),
            new Promise(function (resolve, reject) {
                resolveLocalFileSystemURL(targetDirName, resolve, reject);
            })
        ]).then(function (files) {
            var sourceFile = files[0];
            var targetDir = files[1];

            return new Promise(function (resolve, reject) {
                targetDir.getFile(db_name, {}, resolve, reject);
            }).then(function () {
                console.log("file already copied");
            }).catch(function () {
                console.log("file doesn't exist, copying it");
                return new Promise(function (resolve, reject) {
                    sourceFile.copyTo(targetDir, db_name, resolve, reject);
                }).then(function () {
                    console.log("database file copied");
                });
            });
        });
    }
    $ionicPlatform.openDB = function()
    {

        if(isIOS){
            db = window.sqlitePlugin.openDatabase({name: db_name, iosDatabaseLocation: 'defaut'});
        } else {
            db = window.sqlitePlugin.openDatabase({name: db_name, location: 'default'});
        }
        return db;
    }



     // This function is called later on on the code.
    $ionicPlatform.runDbQuery = function(query)
    {
        db = $ionicPlatform.openDB();
        db.readTransaction(function (txn) {
            txn.executeSql(query, [], function (tx, res) {
                console.log(JSON.stringify(res));
                return res;
            });
        });
    }
    //code continues…

Like I mentioned before, this same code above worked just fine last week on Android and now it doesn't. Actually, the part where it copies the pre-populated database works just fine (callback logs show it).

I would like to know if more people have encountered this annoying issue and please guide me through the right path.

Thanks in advance.


Solution

  • I do not know why but:

    $ sudo ionic plugins rm cordova-plugin-sqlite-2
    $ sudo ionic plugins add cordova-plugin-sqlite-2
    

    It appears something happens when you build for one of the contrary OSs (iOS & Android) that something stops working the right way.