getting values from sqlite database and insert into a array (Javascript)

2,647 views
Skip to first unread message

Arnold Tagne

unread,
Dec 23, 2014, 7:46:38 AM12/23/14
to phon...@googlegroups.com

I've made a function myFunction which performs simple task i.e. to retrieve data from sqlite database and save it in a array which can be used easily. This function should follow these steps to fulfill my functionality.

  1. To retrieve data from database.
  2. To save it into array.
  3. return array

But it is performing these steps in this sequences; 3, 1, 2, so I am not getting the data because it returns without getting it.

Kerri Shotts

unread,
Dec 23, 2014, 10:33:17 AM12/23/14
to phon...@googlegroups.com
Accessing the database is asynchronous. Although you haven't provided any code (please do so in the future with any posts regarding issues), I'm guessing you're returning the array without waiting for the SQLite query to complete.

I'm going to suggest promises for this -- life is much simpler with them. I use Q (https://github.com/kriskowal/q), but there are lots of great libraries out there (or ES6 has them natively, but you'll have to transpile from ES6-->ES5).

Then you can do something like this (this was quick, so I can't promise it isn't without error):

doQuery = function doQuery(db, sqlText, sqlBinds) {
    var deferred = Q.defer();
    db.transaction( function gotTransaction(tx) {
        tx.execute(sqlText, sqlBinds, 
                              function gotResults (tx,results) { deferred.resolve(results); },
                              function gotError (tx, err) { deferred.reject(err); });
        }
    });
    return deferred.promise;
};

function myFunction(db} {
    return doQuery(db, "your query", [binds])
    .then (function processResults® {
        var retArray = [];
        for (var i=0;i<r.rows.length;i++) {
            retArray.push(r.rows.item(i));
        }
        return retArray;
    });
}

myFunction(db)
    .then(function(results) {
        console.log(results);  // logs your results (hopefully)
    })
    .catch(function(err) {
        console.log("Error:", err);
    })
    .done();

Arnold Tagne

unread,
Dec 24, 2014, 3:24:33 AM12/24/14
to phon...@googlegroups.com
Thanks Kerri Shotts.
I am not getting it easily with the code you used. Please could we work my attempt (code)


function myFunction() {

  var catgoryResult = db.transaction(function(tx) {
tx.executeSql(

        "SELECT s.*, a.*, l.*,c.* FROM 'songs' s " +

        "LEFT JOIN 'categories' c ON (s.id_category = c.id_category )" + 

        "LEFT JOIN 'song_artist' sa ON (sa.'id_song' = s.'id_song' AND sa.'is_main_artist' = 1)" + 

        "LEFT JOIN 'artists' a ON (a.'id_artist' = sa.'id_artist') " +
        "LEFT JOIN 'lyrics' l ON (l.'id_song' = s.'id_song')", [], function(tx, result) {

var dataset = result.rows;

for (var i = 0, item = null; i &lt; dataset.length; i++) {

item = dataset.item(i);

                    dataCollected.push(item);
 
               alert(dataCollected.length);

}
$('#results').append();
});
});
       
  alert(dataCollected.length);
    return dataCollected;
}

Arnold Tagne

unread,
Dec 24, 2014, 4:05:39 AM12/24/14
to phon...@googlegroups.com
From the code Kerri Shotts , the "processResults" function is not quite clear because it takes no parameter and definitely the "r" is undefined  and "r.rows" too.
Reply all
Reply to author
Forward
0 new messages