问题
I've got a table called WorkoutTable: http://postimg.org/image/8rw5t7u0n/
I've got a table called DateofWorkout: http://postimg.org/image/9tqjkyszv/be35531c/
Within the DateofWorkout Table i've got the column called 'workout_id_2' which references the column called 'workout_id' in my WorkoutTable.
Basically i want to produce a table which shows the DateofWorkoutTable but instead of 'workout_id_2' i want it to show the NAMES of the workouts.
According to my mock up the following SQL code does this:
SELECT date_of_workout_id, date_of_workout, workout_name FROM WorkoutTable INNER JOIN DateofWorkout ON WorkoutTable.workout_id = DateofWorkout.workout_id_2 ORDER BY date_of_workout_id
According to my mock up both an INNER JOIN and a LEFT JOIN will work.
Furthermore I'm trying to read this out of my actual database in my android project:
public String test(String WorkoutSelectedNameInfo) {
// TODO Auto-generated method stub
String EquivofWorkoutID = "";
String dateofworkoutid = "";
String dateofworkout = "";
open();
ourDatabase = ourhelper.getReadableDatabase();
Cursor c = ourDatabase.rawQuery("SELECT date_of_workout_id, date_of_workout,
workout_name FROM WorkoutTable LEFT JOIN DateofWorkout ON WorkoutTable.workout_id =
DateofWorkout.workout_id_2 ORDER BY date_of_workout_id", null);
if (c.moveToFirst()) {
do {
EquivofWorkoutID = EquivofWorkoutID + c.getString(c.getColumnIndex(STRING_WORKOUT))+ "\n" ;
dateofworkoutid = dateofworkoutid + c.getString(c.getColumnIndex(KEY_DATE_OF_WORKOUT))+ "\n" ;
dateofworkout = dateofworkout + c.getString(c.getColumnIndex(KEY_DATE))+ "\n" ;
} while (c.moveToNext());
}
c.close();
ourDatabase.close();
System.out.println(EquivofWorkoutID);
System.out.println("the id date of the workout is" + dateofworkoutid );
System.out.println("the date of the workout is" + dateofworkout );
return EquivofWorkoutID;
}
As you can see i set up my code to output the values of EquivofWorkoutID, dateofworkoutid and dateofworkout. However, on execution the 'EquivofWorkoutID' actually just displays whatever is present in the WorkoutTable's STRING_WORKOUT column instead of the JOINED table. Furthermore the dateofworkoutid and dateofworkout remain as 'null' suggesting that the cursor hasn't implemented the INNER JOIN statement. How do i read out the information shown using the SQL query shown in the image? Thank you in advance!
来源:https://stackoverflow.com/questions/20889763/using-a-cursor-and-returning-data-after-joining-two-tables