1. Download our Official Android App: Forums for Android!

Apps sqlite query random example

Discussion in 'Android Development' started by ronnixw, Jul 3, 2011.

  1. ronnixw

    ronnixw Newbie
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    13
    Joined:
    Jun 26, 2011

    Jun 26, 2011
    13
    0
    15
    Developer
    Latvia
    Hello,

    can you please show me an example how to select random rows from database using :
    db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy)

    best regards,
    Uldis
     

    Advertisement

  2. miXer

    miXer Android Enthusiast
    Rank:
    None
    Points:
    53
    Posts:
    313
    Joined:
    Aug 12, 2010

    Aug 12, 2010
    313
    51
    53
    Norway
    Code (Text):
    1. Random r = new Random();
    2. db.query(true, DB_TABLE, new String[] { "_id" }, "_id = ?", new String[] { String.valueOf(r.nextInt(100)) }, null, null, null, null);
     
  3. ronnixw

    ronnixw Newbie
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    13
    Joined:
    Jun 26, 2011

    Jun 26, 2011
    13
    0
    15
    Developer
    Latvia
    Thank you , but if I am true , this is not the way I was asking for.
    I wanted sqlite variant not java.

    If I understand by using this method , I need to know how much records are into database and they must be without gaps between them?!
    And this query will return one row or row group?

    best regards,
    ronnixw
     
  4. miXer

    miXer Android Enthusiast
    Rank:
    None
    Points:
    53
    Posts:
    313
    Joined:
    Aug 12, 2010

    Aug 12, 2010
    313
    51
    53
    Norway
    You asked for this method in your question, and this gives you a random row. To get the row count so you dont get IndexOutOfBounds use this:
    Code (Text):
    1. Random r = new Random();
    2. db.query(true, DB_TABLE, new String[] { "_id" }, "_id = ?", new String[] { String.valueOf(r.nextInt(getRowCount())) }, null, null, null, null);
    Code (Text):
    1. private int getRowCount()
    2. {
    3.     Cursor c = db.query(DB_TABLE, new String[] { "_id" }, null, null, null, null, null);
    4.     int count = c.getCount();
    5.     c.close();
    6.     return count;
    7. }
     
  5. GIR

    GIR Well-Known Member
    Rank:
    None
    Points:
    18
    Posts:
    96
    Joined:
    Feb 14, 2010

    Feb 14, 2010
    96
    10
    18
    Classified.
    UK
    Search google for the 'Random Quotes' example, it uses SQLite and has code examples that do this very task.

    From what I currently understand the java code in an android app seems to 'wrap up' the sql commands. I may be wrong, if so please correct me.
     
  6. ronnixw

    ronnixw Newbie
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    13
    Joined:
    Jun 26, 2011

    Jun 26, 2011
    13
    0
    15
    Developer
    Latvia
    Sorry , I asked for random rowS , it means one query return more than one row in random order ;)

    Didn't find better solution than just execute plain sql code :D
     

Share This Page

Loading...