Cordova-sqlitePlugin无法打开数据库

时间:2016-10-12 21:00:41

标签: android cordova ionic-framework cordova-plugins

我目前正在使用Ionic Framework进行混合应用。

上周一切都运行正常,没有从插件中修改任何东西它只是停止工作,我没有找到任何解决方案或足够好的方法的任何线程(只有一个)。

我在Android和iOS设备上运行。

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控制台日志显示不多(根本没有)。

Cordova安装的插件:

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"

实施

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…

就像我之前提到的,上面这个相同的代码上周在Android上工作得很好,现在却没有。 实际上,它复制预填充数据库的部分工作得很好(回调日志显示它)。

我想知道是否有更多人遇到这个恼人的问题,请指导我走正确的道路。

提前致谢。

1 个答案:

答案 0 :(得分:2)

我不知道为什么,但是:

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

当你为一个相反的操作系统(iOS和Android)构建一些东西停止以正确的方式工作时,似乎会发生一些事情。