Android,实施SQLite
这些是我的表格:
http://postimg.org/image/jafsx39h7/
我有代码:
public String getWorkoutNameInfo() {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_DATE_OF_WORKOUT,
KEY_WORKOUT_NAME, KEY_DATE };
Cursor c = ourDatabase.query(TABLE_DATE_WORKOUT, columns, null, null,
null, null, null, null);
String workoutName2 = "";
int iWorkoutID = c.getColumnIndex(KEY_WORKOUT_NAME);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
workoutName2 = workoutName2 + c.getString(iWorkoutID);
}
return workoutName2;
}
这将返回workoutName2,即'2 1 2'
现在我需要在我的WorkoutTable中查找2的值并返回'Back'并生成'Back,Chest,Back',这样我就可以把它放到我的屏幕而不是2 1 2.
我知道我会使用JOIN声明吗?但是我没有运气实现它。
我的表格编码:
// WORKOUT TABLE - COLUMN NAMES
public static final String KEY_WORKOUT = "workout_id";
public static final String STRING_WORKOUT = "workout_name";
// DATE OF WORKOUT TABLE - COLUMN NAMES
public static final String KEY_DATE_OF_WORKOUT = "date_of_workout_id";
public static final String KEY_DATE = "date_of_workout";
public static final String KEY_WORKOUT_NAME = "workout_id";
//TABLE NAMES
private static final String TABLE_WORKOUT = "WorkoutTable";
private static final String TABLE_DATE_WORKOUT = "DateofWorkout";
这是我的尝试:
public String test(String workoutSelectedNameInfo) {
// TODO Auto-generated method stub
String NAMEofWorkout = "";
open();
ourDatabase = ourhelper.getReadableDatabase();
Cursor c = ourDatabase
.rawQuery(
"SELECT * FROM WorkoutTable LEFT JOIN DateofWorkout ON (WorkoutTable.workout_id = DateofWorkout.workout_id) WHERE workout_id = ?",
new String[] { "2" });
int iDateofWorkoutsWorkoutId = c.getColumnIndex(KEY_WORKOUT_NAME);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
NAMEofWorkout = NAMEofWorkout + c.getString(iDateofWorkoutsWorkoutId);
}
c.close();
ourDatabase.close();
System.out.println(NAMEofWorkout);
return NAMEofWorkout;
}
然而当它输出'NameofWorkout'时,它应该是胸部,背部,胸部我什么也得不到,绝对是空白的。
答案 0 :(得分:0)
当我将数据输入到我的机器上的SQLite实例中然后执行查询时,我收到以下错误:
错误:列名不明确:workout_id
在查询中,将WHERE workout_id
更改为WHERE DateofWorkout.workout_id
会使错误消失。
发生此问题是因为结果中有两列名为workout_id
,您必须消除后续引用的歧义。因此,您可能还必须更改
c.getColumnIndex(KEY_WORKOUT_NAME)
到
c.getColumnIndex(TABLE_DATE_WORKOUT + "." + KEY_WORKOUT_NAME)
稍后再使用结果。