我必须在我的数据库训练和练习中使用以下方法getAllWorkouts和getAllExercises。对,这些方法分别进行锻炼和练习。我如何将这两者结合在一起?所以训练将使所有练习加入其中。这是1:M的关系。
getAllWorkouts
public ArrayList<Workout> getAllWokouts(){
ArrayList<Workout> workoutArrayList = new ArrayList<Workout>();
SQLiteDatabase db = this.getReadableDatabase();
try{
String selectQuery = "SELECT * FROM " + TABLE_WORKOUT;
db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
if (c != null && c.moveToFirst()){
do {
Workout workout = new Workout();
workout.setId(c.getInt(0));
workout.setName(c.getString(1));
workoutArrayList.add(workout);
} while (c.moveToNext());
}
return workoutArrayList;
} catch (SQLiteException se){
Log.v("Exception",
Log.getStackTraceString(se));
} catch (Exception e){
Log.v("Exception",
Log.getStackTraceString(e));
} finally {
db.close();
}
return workoutArrayList;
}
getAllExercises
ArrayList<Exercise> exerciseArrayList = new ArrayList<Exercise>();
SQLiteDatabase db = this.getReadableDatabase();
try{
String selectQuery = "SELECT * FROM " + TABLE_EXERCISE;
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor != null && cursor.moveToFirst()){
do {
Exercise exercise = new Exercise();
exercise.setId(cursor.getInt(0));
exercise.setName(cursor.getString(1));
exercise.setSet(cursor.getString(2));
exercise.setWeight(cursor.getString(3));
exercise.setReps(cursor.getString(4));
exerciseArrayList.add(exercise);
} while (cursor.moveToNext());
}
return exerciseArrayList;
} catch (SQLiteException se){
Log.v("Exception",
Log.getStackTraceString(se));
} catch (Exception e){
Log.v("Exception",
Log.getStackTraceString(e));
} finally {
db.close();
}
return exerciseArrayList;
}
我使用了以下代码
public Cursor getAllWorkoutsExercises(){
SQLiteDatabase db = this.getReadableDatabase();
String SELECT_QUERY = "SELECT * FROM '" + TABLE_WORKOUT + "'JOIN'" + TABLE_EXERCISE + "' ON '" + EXERCISE_ID + "' GROUP BY '" + EXERCISE_ID + "'ORDER BY'" + WORKOUT_NAME;
Cursor mCursor = db.rawQuery(SELECT_QUERY, null);
return mCursor;
}
答案 0 :(得分:0)
试试这个:
Cursor mCursor = db.rawQuery("SELECT * FROM Table1, Table2 " +
"WHERE Table1.id = Table2.id_table1 " +
"GROUP BY Table1.data1", null);