public Step selectStep(int stepId) throws Exception { SQLiteDatabase database = dbHelper.getReadableDatabase(); Step result = new Step(); try { Cursor cur = database.query( DbHelper.STEPS, new String[] { DbHelper.STEP_ID, DbHelper.STEP_NAME, DbHelper.STEP_ORDINAL, DbHelper.STEP_SECONDS }, DbHelper.STEP_ID + "=?", new String[] {Integer.toString(stepId)}, null, null, DbHelper.STEP_ID); if (!cur.moveToFirst()) { throw new Exception("No step found with step identity of " + Integer.toString(stepId)); } result.setId(cur.getInt(0)); result.setName(cur.getString(1)); result.setOrdinal(cur.getInt(2)); result.setDurationSeconds(cur.getLong(3)); } catch (Exception e) { database.close(); throw e; } database.close(); return result; }
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; }