Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I have a cross platform app built using AngularJS. I have implemented a SQLite Database and I can create tables and save data to the tables.

I have a view where I need to display data from several tables in. So what I try to do is read data from the tables and save to arrays to be used in the apps various views. I am trying to automate this process and to that I have created a function where I pass in the Table Name to query and the Array to save that tables data to. This function fires onDeviceReady() and loops through all the tables and should ultimately populate all the relevant arrays.

A sample of array to save to as well as Table-Array association is below:

// Arrays to hold Database data
$scope.Array1 = [];
$scope.Array2 = [];

// Create Table names and associated Array names
$scope.tableArrayAssociation = [{
    tablename: "my_table_1",
    arrayname: "Array1"
}, {
    tablename: "my_table_2",
    arrayname: "Array2"
}];

function onDeviceReady()
{
    // Query the Database Tables and populated Arrays
    queryLocalStorageTablesAndBuildArrays($scope.tableArrayAssociation);
}

I then have a function that loops through the $scope.tableArrayAssociation and for each object I pass the Table name to read from and the Array to save to.

function queryLocalStorageTablesAndBuildArrays(tableArrayAssociation) {
    for (var i = 0; i < $scope.tableArrayAssociation.length; i++) {
        queryTable($scope.tableArrayAssociation[i].tablename, $scope.tableArrayAssociation[i].arrayname); // OK
    }
}

So far so good. The issue comes when the query returns values and I try and populate the array values - I am not sure how to pass the Array Name from the queryTable() function to the querySuccess() callback.

I am trying to implement something similar to THIS solution but my alert (or any other code I add) in the querySuccess() function below is never executed.

function queryTable(tableName, arrayName) {
    var sql = 'SELECT * FROM ' + tableName + ''; // OK

    db.transaction(function (tx) {
        db.executeSql(sql, [],
            (function (arrayName) {
                return function (tx, results) {
                querySuccess(tx, results, arrayName);
            };
        })(arrayName), errorCB);
    });
}

function querySuccess(tx, results, arrayName) {
    alert("Getting here..."); // This is never executed
    var len = results.rows.length; // This is never executed
    }
}

Can anyone spot where I am going wrong with my db.executeSql() function?

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.