2

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 .

4
  • 1
    what type of object is limit (int, string, etc)? Commented Jul 20, 2013 at 6:43
  • it is String , i have passed as the parameter to the method in the code , please refer . Commented Jul 20, 2013 at 7:02
  • Did my answer not work for you with the explicit string of 1? Commented Jul 20, 2013 at 7:05
  • No i have tried but it is returning only the first row . Commented Jul 20, 2013 at 7:08

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

7 Comments

but i want to pass the limit dynamically , i mean i have some calculation to find the limit . So i could not give as "1" . Please advice .
I know it is a dumb question but I have to ask, do you have more than one row in your table?
@VIGNESH Can you try the rawQuery that I posted and see if it is doing the same thing to you?
but it requires API level of 16 (i.e) Jelly bean .
Wait, rawQuery does not require API 16, it was added in API 1
|

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.