如何在SQLite中获取随机数据?

时间:2015-04-23 09:40:55

标签: android sql eclipse sqlite

我正在尝试使用Order by NEWID()获取随机顺序,如下所示:

public Cursor getSpecialList(String bloodCategory) {        
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cur = db.rawQuery("SELECT " + colName + " ," +
        "" + colFather + " ," +
        "" + colFamily + " ,"+
        "" + colPhone + " from " + ListInfoTable +
        " WHERE " + colBloodCategory + "='" + bloodCategory + "'" +
        " order by NEWID() ", new String[]{});    
    return cur;     
}

SQLite 日志给出了以下错误:

  

没有这样的功能NEWID

1 个答案:

答案 0 :(得分:2)

您可以将以下代码用于随机数据:

public Cursor getSpecialList(String bloodCategory )
        {       

      SQLiteDatabase db=this.getReadableDatabase();
      Cursor cur=db.rawQuery("SELECT "+colName+" ,"+
            ""+colFather+" ,"+
            ""+colFamily+" ,"+
            ""+colPhone+" from "+ListInfoTable+
            " WHERE "+colBloodCategory+"='"+bloodCategory+"'"+
            " ORDER BY RANDOM() ",new String[] {});    
      return cur;       
    }