VIGNESH
VIGNESH

Reputation: 2033

Selecting Random rows with limit in SQLite

I have went through many related threads and i was not successful yet .

I dont know where i am going wrong , i have the following method to get the rows .

    public Cursor getallrows (String fid , String sid , String bid , String pid , String limit)
    {
        return db.query(DATABASE_ALLPENLIST, new String[] {"rowpos","Peninnerid","issampled","onesymbol","twosymbol","threesymbol","foursymbol","fivesymbol","buildingid","farmid","sampleid","penid"}, "farmid = "+fid+" and sampleid = "+sid+" and buildingid = "+bid+" and penid = "+pid , null, null, null,"RANDOM()",limit);
    }

but the above method returns me the correct number of rows mentioned in the limit but not in the Random order , it is returning the first n rows mentioned in the limit variable .

Is this RANDOM() case-sensitive ? But i have also tried with Random() and "RANDOM() LIMIT "+limit in the place of 7th parameter .

Thanks in Advance .

Upvotes: 2

Views: 824

Answers (1)

ObieMD5
ObieMD5

Reputation: 2657

This query should work with return random rows based off if the limit variable is of string type or you can be explicit with it like this:

db.query(DATABASE_ALLPENLIST, 
         new String[] {"rowpos","Peninnerid","issampled","onesymbol","twosymbol","threesymbol","foursymbol","fivesymbol","buildingid","farmid","sampleid","penid"}, 
         "farmid = "+fid+" and sampleid = "+sid+" and buildingid = "+bid+" and penid = "+pid ,
         null, 
         null, 
         null,
         "RANDOM()",
         "1");

or try raw query:

db.rawQuery("Select rowpos, Peninnerid , issampled, onesymbol, twosymbol, threesymbol, foursymbol, fivesymbol, buildingid, farmid, sampleid, penid From " + DATABASE_ALLPENLIST + " Order By RANDOM() LIMIT ?", new String[] {limit});

Doumentation to this API: Link

Upvotes: 1

Related Questions