コード例 #1
0
  public Routine selectRoutine(int routineId) throws Exception {
    final String sql =
        "SELECT "
            + DbHelper.ROUTINES
            + "."
            + DbHelper.ROUTINE_ID
            + ", "
            + DbHelper.ROUTINE_NAME
            + ", "
            + DbHelper.STEP_ID
            + ", "
            + DbHelper.STEP_NAME
            + ", "
            + DbHelper.STEP_SECONDS
            + ", "
            + DbHelper.STEP_ORDINAL
            + " "
            + " FROM "
            + DbHelper.ROUTINES
            + " JOIN "
            + DbHelper.STEPS
            + " ON "
            + DbHelper.STEPS
            + "."
            + DbHelper.STEP_ROUTINE_ID
            + " = "
            + DbHelper.ROUTINES
            + "."
            + DbHelper.ROUTINE_ID
            + " "
            + " WHERE "
            + DbHelper.ROUTINES
            + "."
            + DbHelper.ROUTINE_ID
            + " = ? "
            + " ORDER BY "
            + DbHelper.STEP_ORDINAL;
    Routine result = null;
    SQLiteDatabase database = dbHelper.getReadableDatabase();
    try {
      Cursor cur = database.rawQuery(sql, new String[] {String.format("%d", routineId)});
      if (!cur.moveToFirst()) {
        database.close();
        return result;
      }
      result = new Routine(cur.getString(1), "");
      result.setId(cur.getInt(0));
      long totalDuration = 0;
      ArrayList<Step> steps = new ArrayList<Step>();
      do {
        Step step = new Step();
        step.setId(cur.getInt(2));
        step.setName(cur.getString(3));
        step.setDurationSeconds(cur.getLong(4));
        step.setOrdinal(cur.getInt(5));
        totalDuration += step.getDurationSeconds();
        steps.add(step);
      } while (cur.moveToNext());

      result.setDurationInSeconds(totalDuration);
      result.setSteps(steps);

      database.close();
    } catch (Exception e) {
      database.close();
      throw e;
    }
    return result;
  }