public static Shift load(Context aContext, int aId) { ShiftCalendarDbOpenHelper dbOpener = new ShiftCalendarDbOpenHelper(aContext); SQLiteDatabase db = dbOpener.getReadableDatabase(); Cursor query = db.query(TABLE_NAME, null, "id=" + String.valueOf(aId), new String[0], null, null, null); try { if (query.moveToNext()) { Shift shift = new Shift( query.getInt(query.getColumnIndex(ID_COLUMN)), query.getString(query.getColumnIndex(NAME_COLUMN)), query.getInt(query.getColumnIndex(COLOUR_COLUMN)), query.getString(query.getColumnIndex(TIME_FROM_COLUMN)), query.getString(query.getColumnIndex(TIME_TO_COLUMN)), query.getInt(query.getColumnIndex(SORT_ORDER_COLUMN))); db.close(); dbOpener.close(); return shift; } else { db.close(); dbOpener.close(); return null; } } finally { query.close(); } }
public void delete(Context aContext) { ShiftCalendarDbOpenHelper dbOpener = new ShiftCalendarDbOpenHelper(aContext); SQLiteDatabase db = dbOpener.getWritableDatabase(); db.delete(TABLE_NAME, ID_COLUMN + "=" + getId(), null); db.delete(ShiftCalendar.TABLE_NAME, ShiftCalendar.SHIFT_ID_COLUMN + "=" + getId(), null); db.close(); dbOpener.close(); }
public void save(Context aContext) { if (mSortOrder < 0) { mSortOrder = loadAll(aContext).size(); } ShiftCalendarDbOpenHelper dbOpener = new ShiftCalendarDbOpenHelper(aContext); SQLiteDatabase db = dbOpener.getWritableDatabase(); ContentValues contentValues = toContentValues(); contentValues.remove(ID_COLUMN); if (getId() < 0) { mId = Long.valueOf(db.insert(TABLE_NAME, null, contentValues)).intValue(); } else { Long.valueOf(db.update(TABLE_NAME, contentValues, ID_COLUMN + "=" + getId(), null)) .intValue(); } db.close(); dbOpener.close(); }