Mohamad Mahmoud Darwish
Mohamad Mahmoud Darwish

Reputation: 4175

How to get random data in SQLite?

I am trying to get a random order by using Order by NEWID() as the following:

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 Log gives me the following error:

No such function NEWID

Upvotes: 0

Views: 2096

Answers (1)

BSavaliya
BSavaliya

Reputation: 827

You can use following code for random data:

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

Upvotes: 1

Related Questions