public List<Exercise> getAllExerciseDetails() { List<Exercise> exercises = new ArrayList<>(); String selectQuery = "SELECT " + KEY_ID + ", " + KEY_NAME + ", " + KEY_DESCRIPTION + ", " + TABLE_EXERCISE_CATEGORY + KEY_NAME + " FROM " + TABLE_EXERCISE_DETAIL + " INNER JOIN " + TABLE_EXERCISE_DETAIL_CATEGORY + " ON " + KEY_ID + " = " + TABLE_EXERCISE_DETAIL + KEY_ID; SQLiteDatabase db = this.getWritableDatabase(); Cursor c = db.rawQuery(selectQuery, null); int id; Exercise exercise; if (c.moveToFirst()) { do { id = c.getInt(c.getColumnIndex(KEY_ID)); exercise = Exercise.getById(exercises, id); if (exercise == null) { exercise = new Exercise(); exercise.setId(id); exercise.setName(c.getString(c.getColumnIndex(KEY_NAME))); exercise.setDescription(c.getString(c.getColumnIndex(KEY_DESCRIPTION))); exercise .getCategories() .add( Category.valueOf( c.getString(c.getColumnIndex(TABLE_EXERCISE_CATEGORY + KEY_NAME)))); exercises.add(exercise); } else { exercise .getCategories() .add( Category.valueOf( c.getString(c.getColumnIndex(TABLE_EXERCISE_CATEGORY + KEY_NAME)))); } } while (c.moveToNext()); } c.close(); return exercises; }
public void updateExerciseDetail(Exercise exercise) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_DESCRIPTION, exercise.getDescription()); db.update(TABLE_EXERCISE_DETAIL, values, KEY_NAME + " = ?", new String[] {exercise.getName()}); deleteExerciseDetailCategory(exercise.getId()); for (Category category : exercise.getCategories()) { createExerciseDetailCategory(exercise.getId(), category); } }
public void createExerciseDetail(Exercise exercise) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_NAME, exercise.getName()); values.put(KEY_DESCRIPTION, exercise.getDescription()); exercise.setId((int) db.insert(TABLE_EXERCISE_DETAIL, null, values)); for (Category category : exercise.getCategories()) { createExerciseDetailCategory(exercise.getId(), category); } }