在SQLite中选择带限制的随机行

时间:2013-07-20 06:36:01

标签: android database sqlite random

我已经完成了许多相关的主题,但我还没有成功。

我不知道我哪里出错了,我有以下方法来获取行。

    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);
    }

但是上面的方法返回了限制中提到的正确行数但不是随机顺序,它返回limit变量中提到的前n行。

RANDOM()是否区分大小写?但我也尝试用Random()"RANDOM() LIMIT "+limit代替第7个参数。

先谢谢。

1 个答案:

答案 0 :(得分:1)

如果limit变量是字符串类型,或者你可以像这样明确地使用它,那么这个查询应该与返回随机行有关:

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");

或尝试原始查询:

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});

此API的版权:Link