新事务正在等待打开操作



我正在使用cordova+angular.js+onsenui创建移动应用程序。

我想做的是打开预填充的数据库,执行选择语句并将结果显示为列表。我使用 litehelpers/cordova-sqlite-ext 插件来操作数据库。

但是当我使用 iOS 模拟器运行应用程序并在 Safari 控制台中看到日志时,会记录"新事务正在等待打开操作"。

控制台日志如下:

数据库已打开:测试.db

测试1

新事务正在等待打开操作

数据库打开:测试.db

我的代码(索引.js)如下。

angular.module('app').controller('listController', function ($scope) {

    var items = [];
    ons.ready(function() {
        db = sqlitePlugin.openDatabase({name: "test.db", location: 2, createFromLocation: 1});
        console.log('test1');
        db.readTransaction(function(tx) {
            console.log('test2');
            var sql = "SELECT id, title, status FROM items";
            tx.executeSql(sql, [], function(tx, response) {
                for (var i = 0; i < response.rows.length; i++) {
                    var row = response.rows.item(i);
                    items.push({title:"Item Title", label:"6h", desc:row.title});
                }
            }, function(error) {
                console.log('SELECT error: ' + error.message);
            });
        }, errorCB, successCB);
    });
    function successCB() {
        $scope.itemTable = items;
        $scope.$apply();
    }
    function errorCB(err) {
        alert("Error processing SQL: "+err.code);
    }
    $scope.showDetail = function(item) {
        console.log('showDetail');
        myNavigator.pushPage("detail.html", {item: item});
    };
});

有谁知道如何解决这个问题?

我感谢任何建议和提示。

对于遇到相同问题的人,我写我的解决方案。

我将插件更改为以下内容。

  • litehelpers/Cordova-sqlite-storage
  • an-rahulpandey/cordova-plugin-dbcopy

我的代码如下所示。

function onDeviceReady() {
    // Handle the Cordova pause and resume events
    document.addEventListener( 'pause', onPause.bind( this ), false );
    document.addEventListener( 'resume', onResume.bind( this ), false );
    dbcopy();
}
function dbcopy()
{
    window.plugins.sqlDB.copy("test.db", 0, copysuccess, copyerror);
}
function copysuccess()
{
    //open db and run your queries
    //alert('copysuccess');
    openDatabase();
}
function openDatabase()
{
    db = window.sqlitePlugin.openDatabase({name: "test.db"});
}
function copyerror(e)
{
        //db already exists or problem in copying the db file. Check the Log.
    console.log("Error Code = "+JSON.stringify(e));
    //e.code = 516 => if db exists
    if (e.code == '516') {
        openDatabase();
    }
}
angular.module('app').controller('listController', function ($scope) {
    var items = [];
    ons.ready(function() {
        if (db === null) {
            openDatabase();
        }
        db.transaction(function(tx) {
            var sql = "SELECT * FROM items;";
            tx.executeSql(sql, [], function(tx, response) {
                for (var i = 0; i < response.rows.length; i++) {
                    var row = response.rows.item(i);
                    items.push({title:row.title, label:row.label, desc:row.desc});
                }
                console.log("res.rows.item(0).title: " + response.rows.item(0).title);
            }, errorCB);
        }, errorCB, successCB);
    });
    function successCB() {
        $scope.itemTable = items;
        $scope.$apply();
    }
    function errorCB(err) {
        alert("Error processing SQL: "+err.message);
    }
    $scope.showDetail = function(item) {
        console.log('showDetail');
        myNavigator.pushPage("detail.html", {item: item});
    };
});

最新更新