mercredi 29 juillet 2015

Getting random data from database with conditions

so here I'm trying to get a random question from my table where the column "stat" is 0, anyway the problem here is that it won't accept the random int so c.moveToPosition(rnd) makes the error or I don't now and it causes the app to crush. This's the code please help!

public String[] databasetostring ()  {
    String dbstring = "";
    String optstring = "";
    SQLiteDatabase db = getWritableDatabase();
    String query = "SELECT * FROM " + TABLE_QUESTIONS + " WHERE 1";

    Cursor c = db.rawQuery(query, null);
    c.moveToFirst();

    while (!c.isAfterLast()){
        if (   c.getInt(c.getColumnIndex("stat")) != 1   ){
                dbstring += c.getString(c.getColumnIndex("question"));
                dbstring += "\n";
                optstring += c.getString(c.getColumnIndex("answer"));
                optstring += "\n";

        }
        int rnd = new Random().nextInt(c.getCount());
        c.moveToPosition(rnd);
    }
    db.close();
    c.close();
    return new String[] {dbstring, optstring};

}

This is the logcat, it keeps going into that loop!

07-29 22:40:07.352  10939-10949/com.example.bibiwars.digital D/dalvikvm GC_FOR_ALLOC freed 0K, 23% free 6057K/7824K, paused 20ms, total 20ms
07-29 22:40:07.352  10939-10939/com.example.bibiwars.digital D/dalvikvm GC_FOR_ALLOC freed 2061K, 49% free 3995K/7824K, paused 0ms, total 0ms
07-29 22:40:07.362  10939-10939/com.example.bibiwars.digital D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 39% free 4820K/7824K, paused 10ms, total 10ms
07-29 22:40:07.362  10939-10939/com.example.bibiwars.digital D/dalvikvm﹕ GC_FOR_ALLOC freed 824K, 34% free 5232K/7824K, paused 0ms, total 0ms




Aucun commentaire:

Enregistrer un commentaire