Page 1 of 1

How to you pass variables into a web sql limit clause?

Posted: Wed Jul 23, 2014 6:13 pm
by drayarms
I'm using web sql to store and retrieve data to and from a mobile device's local memory. I wrote the following query for the retrieval part which works just fine

Code: Select all


   			var sql = "SELECT * FROM messages ORDER BY timestamp DESC LIMIT 0,20";
    			
    			transaction.executeSql (sql, undefined, function (transaction, result){

                                     //Get rows

                       });




But I want to use variables in place of the numbers because the code will be reused in several different instances. So my new code reads like this

Code: Select all



                        var index = 0;

                        var lim = 20;

   			var sql = "SELECT * FROM messages ORDER BY timestamp DESC LIMIT index,lim";
    			
    			transaction.executeSql (sql, undefined, function (transaction, result){

                                     //Get rows

                       });




Unfortunately, this doesn't work and I don't even get any errors. Who knows what I'm doing wrong? :whistling:

Re: How to you pass variables into a web sql limit clause?

Posted: Wed Jul 23, 2014 6:48 pm
by requinix
Use the variables to construct the query.

Code: Select all

var sql = "SELECT * FROM messages ORDER BY timestamp DESC LIMIT " + index + ", " + lim;