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

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
drayarms
Forum Contributor
Posts: 134
Joined: Fri Dec 31, 2010 5:11 pm

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

Post 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:
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post by requinix »

Use the variables to construct the query.

Code: Select all

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