2

I want to return the number of rows in a particular table in a database in WebSQL inside a javascript function. Below is my code.

function getCustomerCount(){    
    var count = 0;  
    db.transaction(function(tx) {   
    tx.executeSql('SELECT * FROM contacts', [], function(tx, results) {
             count = results.rows.length;   
        });
    });
    return count;
}


I am new to WebSQL and rather unfamiliar with javascript also.
Any suggestions?

1
  • use the SQL count query SELECT COUNT(*) FROM contacts Commented Oct 2, 2014 at 8:17

2 Answers 2

2

You can't do this:

function getCustomerCount(){    
    var count = 0;  
    db.transaction(function(tx) {   
    tx.executeSql('SELECT * FROM contacts', [], function(tx, results) {
             // this function is called when the executeSql is ended
             count = results.rows.length;   
        });
    });
    // This returns always 0!
    return count;
}

You have to use callbacks like so:

function getCustomerCount( callback ){    
    var count = 0;  
    db.transaction(function(tx) {   
    tx.executeSql('SELECT * FROM contacts', [], function(tx, results) {
             // this function is called when the executeSql is ended
             count = results.rows.length;
             callback( count );   // <-- call the callback when is done   
        });
    });
}

And use it like:

getCustomerCount( function( count ) {
    // do what you want with the count
    console.log( 'The number of rows is: '+ count );
});
Sign up to request clarification or add additional context in comments.

5 Comments

Perhaps it's better to remove the return statement in the second example
The thing is I want to use that count in another location so that I want to return the value. Anything that can be done to return the count?
it's the easier way to retrieve that number
Thanx for the time and support; that I wanted to return a value and use it somewhere I will go with jQuery method.
@Droidy - you can simply call your exampleThatUsesCount(count); method instead of the console.log()
1

Another way is to use promises, here is an example with the Jquery deferred object

function getCustomerCount() {
    //setup the deferred object
    var defer = $.Deferred();
    var count = 0;
    db.transaction(function(tx) {
        tx.executeSql('SELECT * FROM contacts', [], function(tx, results) {
            //resolve the promise passing the count data
            defer.resolve(results.rows.length);
        });
    });

    //return a promise
    return defer.promise();
}

var countPromise = getCustomerCount();

//when the promise is resolved do something with the data
$.when(countPromise).done(function(count) {
    //now use count, could be you call another function that needs to use count,
    exampleThatUsesCount(count);
   //or assign it to another variable, or trigger an event that someone else in you app is    listening for
});


function exampleThatUsesCount(count) {
    //do something that uses count here
}

2 Comments

Why use this method? you are adding complexity that isn't needed!
I was just showing another example of how to handle async processes don't see that being worthy of a down vote. Personally I like promises as I find it easier for my code to grow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.