public static ArrayList<Question> getAllQuestionByType(int type) { ArrayList<Question> questions = new ArrayList<>(); SQLiteDatabase sqLiteDatabase = openConnection(); String query = "SELECT Questions. *, Images.ImageData FROM Questions " + "LEFT JOIN Images ON Questions.ImageId = Images.Id WHERE Questions.Type = ?"; Cursor cursor = sqLiteDatabase.rawQuery(query, new String[] {"" + type}); cursor.moveToFirst(); int tmp = 1; while (!cursor.isAfterLast()) { Question question = new Question(); question.index = tmp; question.id = cursor.getInt(0); question.question = fixQuestion(byteArrayToString(cursor.getBlob(2))); question.answer1 = fixAnswer(cursor.getString(3)); question.answer2 = fixAnswer(cursor.getString(4)); question.answer3 = fixAnswer(cursor.getString(5)); question.answer4 = fixAnswer(cursor.getString(6)); question.correctAnswer = cursor.getInt(7); question.fixedAnswer = cursor.getInt(9); question.isSentFixedAnswer = cursor.getInt(10); question.imageData = cursor.getBlob(11); question.type = cursor.getInt(1); questions.add(question); cursor.moveToNext(); tmp++; } cursor.close(); closeConnection(); return questions; }
public static ArrayList<Question> getQuestionsByTypeAndExamId( int type, int examId, boolean isRandom, int numberOfQuestions) { ArrayList<Question> questions = new ArrayList<>(); SQLiteDatabase sqLiteDatabase = openConnection(); String query; Cursor cursor; if (!isRandom) { query = "select tmp.* from Exams, (SELECT Questions. *, Images.ImageData FROM Questions LEFT JOIN " + "Images ON Questions.ImageId = Images.Id) as tmp where " + "Exams.ExamId=? and Exams.Type=? and Exams.QuestionId=tmp.Id"; cursor = sqLiteDatabase.rawQuery(query, new String[] {"" + examId, "" + type}); } else { query = "SELECT Questions. *, Images.ImageData FROM Questions LEFT JOIN " + "Images ON Questions.ImageId = Images.Id WHERE Questions.Type = ? " + "order by Random() limit " + numberOfQuestions; cursor = sqLiteDatabase.rawQuery(query, new String[] {"" + type}); } cursor.moveToFirst(); int tmp = 1; while (!cursor.isAfterLast()) { Question question = new Question(); question.index = tmp; question.id = cursor.getInt(0); question.question = fixQuestion(byteArrayToString(cursor.getBlob(2))); question.answer1 = fixAnswer(cursor.getString(3)); question.answer2 = fixAnswer(cursor.getString(4)); question.answer3 = fixAnswer(cursor.getString(5)); question.answer4 = fixAnswer(cursor.getString(6)); question.correctAnswer = cursor.getInt(7); question.fixedAnswer = cursor.getInt(9); question.isSentFixedAnswer = cursor.getInt(10); question.imageData = cursor.getBlob(11); question.type = cursor.getInt(1); questions.add(question); cursor.moveToNext(); tmp++; } cursor.close(); closeConnection(); return questions; }