JAVA   124

Untitled

Guest on 21st April 2022 06:15:26 PM

  1. public class MedsDBHelper extends SQLiteOpenHelper {
  2.     Context context;
  3.  
  4.     private static final String DATABASE_NAME = "medsList.db";
  5.  
  6.  
  7.     private static final String TABLE_NAME = "My_Medications";
  8.     private static final String COLUMN_TITLE = "Medication_Name";
  9.     private static final String COLUMN_MG = "Medication_MG";
  10.     private static final String COLUMN_SCHEDULE = "Medication_Schedule";
  11.  
  12.     MedsDBHelper(Context context) {
  13.         super(context, DATABASE_NAME, null, 1 );
  14.         this.context = context;
  15.  
  16.     }
  17.     @Override
  18.     public void onCreate(SQLiteDatabase myDB) {
  19.         String query = "CREATE TABLE " + TABLE_NAME +
  20.                 " (" +
  21.                 COLUMN_TITLE + " TEXT PRIMARY KEY , " +
  22.                 COLUMN_MG + " TEXT, " +
  23.                 COLUMN_SCHEDULE + " TEXT);";
  24.         myDB.execSQL(query);
  25.  
  26.     }
  27.  
  28.     @Override
  29.     public void onUpgrade(SQLiteDatabase myDB, int i, int i1) {
  30.         myDB.execSQL("drop Table if exists " + TABLE_NAME);
  31.         onCreate(myDB);
  32.     }
  33.     public void addMedication(String title, String medsMG, String sched) {
  34.         SQLiteDatabase myDB = this.getWritableDatabase();
  35.         ContentValues cv = new ContentValues();
  36.  
  37.         cv.put(COLUMN_TITLE, title);
  38.         cv.put(COLUMN_MG, medsMG);
  39.         cv.put(COLUMN_SCHEDULE, sched);
  40.         long result = myDB.insert(TABLE_NAME, null, cv);
  41.         if(result == -1) {
  42.             Toast.makeText(context,"Failed", Toast.LENGTH_SHORT).show();
  43.         } else {
  44.             Toast.makeText(context, "Added Successfully!", Toast.LENGTH_SHORT).show();
  45.         }
  46.     }
  47.     Cursor readData() {
  48.         String query = "SELECT * FROM " + TABLE_NAME;
  49.         SQLiteDatabase myDB = this.getReadableDatabase();
  50.         Cursor cursor = null;
  51.         if(myDB != null) {
  52.             cursor = myDB.rawQuery(query, null);
  53.         }
  54.         return cursor;
  55.     }
  56.     public void deleteMedsData(String id) {
  57.         SQLiteDatabase db = this.getWritableDatabase();
  58.         db.delete(TABLE_NAME, " Medication_Name = ?", new String[] {id});
  59.         db.close();
  60.     }
  61.     void updateData(String title, String mg, String schedule) {
  62.         SQLiteDatabase medsDB = this.getWritableDatabase();
  63.         ContentValues cv = new ContentValues();
  64.         cv.put(COLUMN_TITLE, title);
  65.         cv.put(COLUMN_MG, mg);
  66.         cv.put(COLUMN_SCHEDULE, schedule);
  67.  
  68.         long result = medsDB.update(TABLE_NAME, cv,  " Medication_Name = ?", new String[] {title});
  69.         if (result == -1) {
  70.             Toast.makeText(context,"Failed to update", Toast.LENGTH_SHORT).show();
  71.         } else {
  72.             Toast.makeText(context,"Update Successful", Toast.LENGTH_SHORT).show();
  73.         }
  74.     }
  75.  
  76. }

Raw Paste


Login or Register to edit or fork this paste. It's free.