raimtoon
raimtoon

Reputation: 725

new transaction is waiting for open operation

I'm creating mobile app using cordova+angular.js+onsenui.

What I want to do is to open pre-populated database, execute select statement and display result as list. I use litehelpers/cordova-sqlite-ext plugin to manipulate database.

But when I run the app with iOS simulator and see the log in Safari console, "new transaction is waiting for open operation" is logged.

Console log is as below:

database already open: test.db

test1

new transaction is waiting for open operation

DB opened: test.db

My code(index.js) is the following.

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});
    };
});

Does anyone know how to resolve this?

I appreciate any suggestion and hint.

Upvotes: 1

Views: 3011

Answers (1)

raimtoon
raimtoon

Reputation: 725

For someone who has encountered same problem, I write my solution.

I changed plugin to the following.

And my code is like below.

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});
    };
});

Upvotes: 2

Related Questions