jeudi 31 mars 2016

Cordova Android sqlite database query and array shuffle

I apologize for such a long post but this is for our senior project and it is crunchtime so I tried to be as detailed as humanly possible for my skill level. Any and all help would be an absolute lifesaver.

My team and I are developing an Android app in Cordova using Visual Studio that uses a prepopulated sqlite database to query from. We can see that the database file is successfully created during the onDeviceReady function with the following code in our index.js:

// deviceready Event Handler
onDeviceReady: function() {
    $("#page1").show();
    app.receivedEvent('deviceready');

    var db = window.sqlitePlugin.openDatabase({name: 'events.db', createFromLocation: 1, iosDatabaseLocation: 'default'});

then in our project.js file we have the following code:

private function getTopTen() {

    //Opens database file for query
    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor crs = db.rawQuery("SELECT Name FROM events WHERE Park='SomeFakePark' ", null);

    var topTenArray = new Array(10);
    var length = 10;
    int i = 0;

    //Writes the top ten ride/event names into array
    for ( i = 0, i < length, i++) {
        String rideName = crs.getString(crs.getColumnIndex("NAME"));
        topTenArray[i] = rideName;
        crs.moveToNext();
    }
    return topTenArray;
}

^^^^^The function 'getTopTen' above attempts to query the database file for names of rides within a given amusement park. The table and database are both named 'events' and 'events.db' respectively. That table has columns labeled 'Name' and 'Park' and we want to select the first ten entries in column 'Name' that all have the same entry in column 'Park'. For example, starting from the first row of column 'Name' I want to pull the first ten ride names that have the same entry 'SomeFakePark' in the column 'Parks'. Then we want to copy those first ten 'Name' entries into an array for later recall and display.

//Takes a passed array and shuffles it randomly
function randomize(array) {
    var randomArray = [];
    var arrayLength = array.length;
    var i;

    while (counter) {

        i = Math.floor(Math.random() * array.length);

        if (i in array) {
            randomArray.push(array[i]);
            delete array[i];
            counter--;
        }
    }
    return randomArray;
}

^^^^^The above function 'randomize' takes an array (the one created from the first function) and shuffles it randomly.

Finally these two functions are called by a button tap as such:

//Random button actions
$("#rbutton").on("tap", function(){
    randomize(getTopTen());
    navigation(page1, page2);

^^^^^Where the return value of the function randomItinerary() is passed as a parameter for the function randonmize().

I would like to know if this code is on the right track for what I am trying to accomplish? Mainly, is the DB query correct and also when the randomize function finally returns my shuffled array is that array value kept for use to display later or do I need to store it in a permanent global variable somewhere for recall, and if it needs a variable for storing where and how do I create that variable?

Aucun commentaire:

Enregistrer un commentaire