lundi 1 juin 2020

How to use RANDOM() LIMIT in Android Studio to get limited list from random list from total list?

I want to add the RANDOM() LIMIT 5 to above similar question in following places so that I get only 5 random questions from all questions available in database or from specific category.

This question is similar to codes from : String selection filtering by 2 parameters in SQLite

CODE 1: ...............................................................................

public ArrayList<Question> getAllQuests() {
    ArrayList<Question> questionList = new ArrayList<>();
    db = openHelper.getReadableDatabase();
    Cursor c = db.rawQuery("SELECT * FROM " + QuestionsTable.TABLE_NAME, null);

    if (c.moveToFirst()) {
        do { //TODO
    } while (c.moveToNext());
    }

CODE 2: ...............................................................................

public List<Category> getAllCats() {
    List<Category> categoryList = new ArrayList<>();
    db = openHelper.getReadableDatabase();
    Cursor c = db.rawQuery("SELECT * FROM " + CategoriesTable.TABLE_NAME, null);

CODE 3: ...............................................................................

public ArrayList<Question> getQuests(int catID) {
    ArrayList<Question> questionList = new ArrayList<>();
    db = openHelper.getReadableDatabase();

    String selection = QuestionsTable.COLUMN_CAT_ID + " = ? ";
    String[] selectionArgs = new String[]{String.valueOf(catID)};

    Cursor c = db.query(
            QuestionsTable.TABLE_NAME,
            null,
            selection,
            selectionArgs,
            null,
            null,
            null
    );

...............................................................................

The line I tried to add was of this type in all 3 codes;

Cursor c = db.rawQuery("SELECT * FROM " + QuestionsTable.TABLE_NAME +" ORDER BY RANDOM() LIMIT 5", null);

I don't know where I misplaced the code. But the Build Run in Android Studio is working fine. But code didn't work. Any help is grateful.




Aucun commentaire:

Enregistrer un commentaire