如何从Sqlite数据库中选择行的特定范围(例如20到30),然后将其随机化

时间:2018-11-23 09:44:13

标签: android-studio android-sqlite

假设您有一个包含40个问题的数据库

现在可以按顺序选择问题1至10,然后随机选择问题b / w

例如,我有40个问题,它会选择问题1至10,而不是选择10个以上的问题,然后以随机顺序询问问题1至10

    public class QuizHelper extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;
    // Database Name
    private static final String DATABASE_NAME = "mathsone";
    // tasks table name
    private static final String TABLE_QUEST = "quest";
    // tasks Table Columns names
    private static final String KEY_ID = "qid";
    private static final String KEY_QUES = "question";
    private static final String KEY_ANSWER = "answer"; // correct option
    private static final String KEY_OPTA = "opta"; // option a
    private static final String KEY_OPTB = "optb"; // option b
    private static final String KEY_OPTC = "optc"; // option c
    private SQLiteDatabase dbase;
    public QuizHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        dbase = db;
        String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUEST + " ( "
                + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUES
                + " TEXT, " + KEY_ANSWER + " TEXT, " + KEY_OPTA + " TEXT, "
                + KEY_OPTB + " TEXT, " + KEY_OPTC + " TEXT)";
        db.execSQL(sql);
        addQuestion();

    }
    private void addQuestion() {
        Question q1 = new Question("5+2 = ?", "7", "8", "6", "7");
        this.addQuestion(q1);
        Question q2 = new Question("2+18 = ?", "18", "19", "20", "20");
        this.addQuestion(q2);
        Question q3 = new Question("10-3 = ?", "6", "7", "8", "7");
        this.addQuestion(q3);
        Question q4 = new Question("5+7 = ?", "12", "13", "14", "12");
        this.addQuestion(q4);
        Question q5 = new Question("3-1 = ?", "1", "3", "2", "2");
        this.addQuestion(q5);
        Question q6 = new Question("0+1 = ?", "1", "0", "10", "1");
        this.addQuestion(q6);
        Question q7 = new Question("9-9 = ?", "0", "9", "1", "0");
        this.addQuestion(q7);
        Question q8 = new Question("3+6 = ?", "8", "7", "9", "9");
        this.addQuestion(q8);
        Question q9 = new Question("1+5 = ?", "6", "7", "5", "6");
        this.addQuestion(q9);
        Question q10 = new Question("7-5 = ?", "3", "2", "6", "2");
        this.addQuestion(q10);
        Question q11 = new Question("7-2 = ?", "7", "6", "5", "5");
        this.addQuestion(q11);
        Question q12 = new Question("3+5 = ?", "8", "7", "5", "8");
        this.addQuestion(q12);
        Question q13 = new Question("0+6 = ?", "7", "6", "5", "6");
        this.addQuestion(q13);
        Question q14 = new Question("12-10 = ?", "1", "2", "3", "2");
        this.addQuestion(q14);
        Question q15 = new Question("12+2 = ?", "14", "15", "16", "14");
        this.addQuestion(q15);
        Question q16 = new Question("2-1 = ?", "2", "1", "0", "1");
        this.addQuestion(q16);
        Question q17 = new Question("6-6 = ?", "6", "12", "0", "0");
        this.addQuestion(q17);
        Question q18 = new Question("5-1 = ?", "4", "3", "2", "4");
        this.addQuestion(q18);
        Question q19 = new Question("4+2 = ?", "6", "7", "5", "6");
        this.addQuestion(q19);
        Question q20 = new Question("5+1 = ?", "6", "7", "5", "6");
        this.addQuestion(q20);
        Question q21 = new Question("5-4 = ?", "5", "4", "1", "1");
        this.addQuestion(q21);

    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUEST);

        onCreate(db);
    }
    // Adding new question
    public void addQuestion(Question quest) {
        ContentValues values = new ContentValues();
        values.put(KEY_QUES, quest.getQUESTION());
        values.put(KEY_ANSWER, quest.getANSWER());
        values.put(KEY_OPTA, quest.getOPTA());
        values.put(KEY_OPTB, quest.getOPTB());
        values.put(KEY_OPTC, quest.getOPTC());
        dbase.insert(TABLE_QUEST, null, values);
    }
    public List<Question> getAllQuestions() {
        List<Question> quesList = new ArrayList<Question>();
        String selectQuery = "SELECT * FROM " + TABLE_QUEST;
        dbase = this.getReadableDatabase();
        Cursor cursor = dbase.rawQuery(selectQuery, null);
        if (cursor.moveToFirst()) {
            do {
                Question quest = new Question();
                quest.setID(cursor.getInt(0));
                quest.setQUESTION(cursor.getString(1));
                quest.setANSWER(cursor.getString(2));
                quest.setOPTA(cursor.getString(3));
                quest.setOPTB(cursor.getString(4));
                quest.setOPTC(cursor.getString(5));
                quesList.add(quest);
            } while (cursor.moveToNext());
        }

        return quesList;
    }
}

我尝试做

String selectQuery = "SELECT * FROM " + TABLE_QUEST + " ORDER BY RANDOM() LIMIT 10";

但是它仅限制了我想问的问题数量,例如,我希望将问题从20个增加到30个,并且只有那些问题而不是其他问题才能从数据库中以随机顺序问到这些问题

1 个答案:

答案 0 :(得分:0)

您只需要添加 WHERE 子句来限制所选的行,例如:-

SELECT * FROM quest WHERE qid >= 20 and qid <= 30  ORDER BY RANDOM() LIMIT 10;

请考虑以下内容:-

DROP TABLE IF EXISTS quest;
CREATE TABLE IF NOT EXISTS quest (qid INTEGER PRIMARY KEY, question TEXT, answer TEXT, opta TEXT, optb TEXT, optc TEXT);
INSERT INTO quest (question,answer,opta,optb,optc) VALUES 
    ("5+2 = ?", "7", "8", "6", "7"),
    ("2+18 = ?", "18", "19", "20", "20"),
    ("10-3 = ?", "6", "7", "8", "7"),
    ("5+7 = ?", "12", "13", "14", "12"),
    ("3-1 = ?", "1", "3", "2", "2"),
    ("0+1 = ?", "1", "0", "10", "1"),
    ("9-9 = ?", "0", "9", "1", "0"),
    ("3+6 = ?", "8", "7", "9", "9"),
    ("1+5 = ?", "6", "7", "5", "6"),
    ("7-5 = ?", "3", "2", "6", "2"),
    ("7-2 = ?", "7", "6", "5", "5"),
    ("3+5 = ?", "8", "7", "5", "8"),
    ("0+6 = ?", "7", "6", "5", "6"),
    ("12-10 = ?", "1", "2", "3", "2"),
    ("12+2 = ?", "14", "15", "16", "14"),
    ("2-1 = ?", "2", "1", "0", "1"),
    ("6-6 = ?", "6", "12", "0", "0"),
    ("5-1 = ?", "4", "3", "2", "4"),
    ("4+2 = ?", "6", "7", "5", "6"),
    ("5+1 = ?", "6", "7", "5", "6"),
    ("5-4 = ?", "5", "4", "1", "1"),
    ("5+2 = ?", "7", "8", "6", "7"),
    ("2+18 = ?", "18", "19", "20", "20"),
    ("10-3 = ?", "6", "7", "8", "7"),
    ("5+7 = ?", "12", "13", "14", "12"),
    ("3-1 = ?", "1", "3", "2", "2"),
    ("0+1 = ?", "1", "0", "10", "1"),
    ("9-9 = ?", "0", "9", "1", "0"),
    ("3+6 = ?", "8", "7", "9", "9"),
    ("1+5 = ?", "6", "7", "5", "6"),
    ("7-5 = ?", "3", "2", "6", "2"),
    ("7-2 = ?", "7", "6", "5", "5"),
    ("3+5 = ?", "8", "7", "5", "8"),
    ("0+6 = ?", "7", "6", "5", "6"),
    ("12-10 = ?", "1", "2", "3", "2"),
    ("12+2 = ?", "14", "15", "16", "14"),
    ("2-1 = ?", "2", "1", "0", "1"),
    ("6-6 = ?", "6", "12", "0", "0"),
    ("5-1 = ?", "4", "3", "2", "4"),
    ("4+2 = ?", "6", "7", "5", "6"),
    ("5+1 = ?", "6", "7", "5", "6"),
    ("5-4 = ?", "5", "4", "1", "1")
;
SELECT * FROM quest WHERE qid >= 20 and qid <= 30  ORDER BY RANDOM() LIMIT 10;
  • 注意42个问题,但为方便起见,其中一些问题已重复。但是,由于 qid 列对于用作所选行指示符的行是唯一的。
  • 因为范围是20-30(11行),而LIMIT是10,那么将始终忽略1行。

结果可能是:-

enter image description here

结果可能是:-

enter image description here

.......