public bool Remove() { SQLiteDatabase sqlDatabase = null; try { Globals dbHelp = new Globals(); dbHelp.OpenDatabase(); sqlDatabase = dbHelp.GetSQLiteDatabase(); if (sqlDatabase != null && sqlDatabase.IsOpen) { var sql = @"DELETE FROM [MedicationSpread] WHERE ID = " + ID.ToString(); sqlDatabase.ExecSQL(sql); Log.Info(TAG, "Remove: Removed Medication Spread with ID of " + ID.ToString()); if (MedicationTakeTime != null) { MedicationTakeTime.Remove(); } if (MedicationTakeReminder != null) { MedicationTakeReminder.Remove(); } sqlDatabase.Close(); return(true); } Log.Error(TAG, "Remove: SQLite database is null or was not opened - remove failed"); return(false); } catch (Exception e) { Log.Error(TAG, "Remove: Exception - " + e.Message); if (sqlDatabase != null && sqlDatabase.IsOpen) { sqlDatabase.Close(); } return(false); } }
public bool Save(int medicationSpreadID, int medicationID) { SQLiteDatabase sqlDatabase = null; try { Globals dbHelp = new Globals(); dbHelp.OpenDatabase(); sqlDatabase = dbHelp.GetSQLiteDatabase(); if (sqlDatabase != null) { if (sqlDatabase.IsOpen) { if (medicationSpreadID == -1) { Log.Info(TAG, "Save (insert): Saving New Medication Spread"); ContentValues values = new ContentValues(); values.Put("MedicationID", medicationID); values.Put("Dosage", Dosage.ToString()); values.Put("FoodRelevance", (int)FoodRelevance); ID = (int)sqlDatabase.Insert("MedicationSpread", null, values); Log.Info(TAG, "Save (insert): Saved Spread with ID - " + ID.ToString() + ", medicationID - " + medicationID.ToString() + ", Dosage - " + Dosage.ToString() + ", Food Relevance - " + StringHelper.MedicationFoodForConstant(FoodRelevance)); IsNew = false; IsDirty = false; if (MedicationTakeTime != null) { MedicationTakeTime.Save(ID); } if (MedicationTakeReminder != null) { MedicationTakeReminder.Save(ID); } } else { ContentValues values = new ContentValues(); values.Put("MedicationID", medicationID); values.Put("Dosage", Dosage.ToString()); values.Put("FoodRelevance", (int)FoodRelevance); string whereClause = "ID = ? AND MedicationID = ?"; string[] wheres = new string[] { ID.ToString(), MedicationID.ToString() }; sqlDatabase.Update("MedicationSpread", values, whereClause, wheres); if (MedicationTakeTime != null) { MedicationTakeTime.Save(ID); } if (MedicationTakeReminder != null) { MedicationTakeReminder.Save(ID); } IsDirty = false; } sqlDatabase.Close(); return(true); } } return(false); } catch (Exception e) { Log.Error(TAG, "Save: Exception - " + e.Message); if (sqlDatabase != null && sqlDatabase.IsOpen) { sqlDatabase.Close(); } return(false); } }