0

我将数据存储在sqllite中。当我使用带有 where 子句的 sql 查询从数据库中检索数据时。它没有给出任何结果。我检查它甚至必须返回两条记录。

数据库中的数据

exercise_main_muscle=Biceps exercise_equipment=Dumbbell exercise_name=Incline Dumbbell Curl exercise_mechanic=Isolation exercise_other_muscle=Forearms exercise_level=Beginner exercise_type=Strength

exercise_main_muscle=Biceps exercise_equipment=Dumbbell exercise_name=Hammer Curl exercise_mechanic=Isolation exercise_other_muscle=Forearms exercise_level=Beginner exercise_type=Strength`

使用此查询:

select * from exercise where exercise_type = 'Strength' AND exercise_main_muscle = 'Biceps' AND exercise_other_muscle = 'Forearms' AND exercise_equipment = 'Isolation' AND exercise_mechanic = 'Beginner'

代码:

String sql = "select * from " + TABLE_EXERCISE + " where " + KEY_EXERCISE_TYPE + " = '" + exerciseType + "' AND "
                + KEY_MAIN_MUSCLE + " = '" + mainMuscle + "' AND " + KEY_OTHER_MUSCLE + " = '" + otherMuscle + "' AND "
                + KEY_EXERCISE_EQUIPMENT + " = '" + equipment + "' AND " + KEY_MECHANIC + " = '" + mechanic + "'";

Cursor cursor = db.rawQuery(sql, null);

cursor.moveToFirst();

while(!cursor.isAfterLast()) {
    Exercise exercise = cursorToExercise(cursor);
    exerciseList.add(exercise);
    cursor.moveToNext();
}
db.close();

private Exercise cursorToExercise(Cursor cursor) {
    Exercise exercise = new Exercise();
    exercise.setID(cursor.getLong(0));
    exercise.setExerciseName(cursor.getString(1));
    exercise.setExerciseType(cursor.getString(2));
    exercise.setMainMuscle(cursor.getString(3));
    exercise.setOtherMuscle(cursor.getString(4));
    exercise.setExerciseEquipment(cursor.getString(5));
    exercise.setExerciseMechanic(cursor.getString(6));
    exercise.setExerciseLevel(cursor.getString(7));
    return exercise;
}
4

1 回答 1

0

您在数据库中的数据是exercise_mechanic=Isolation

尽管

你的查询是exercise_equipment=Isolation

exercise_mechanic = 'Beginner'

你的查询是错误的。小号

于 2013-07-14T13:23:38.337 回答