title: Displaying data for the correct corresponding SQLite database Android/Java
body: <p>I have a recyclerview with a list of items or "diaries" that have data stored in an sql database "diaries". When I click on a diary, it will take me to an activity where I can add more items or "plants" to a different sql database "plants". I want to make sure I'm displaying the right plants for the corresponding diary that was clicked.</p>
  
  <p>How do I make sure I'm displaying the right plant database when I click a diary? I'm not sure how to link the databases. Thanks...</p>
  
  <pre><code>package com.bawp.babyneeds.data;
  
  import android.content.ContentValues;
  import android.content.Context;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteOpenHelper;
  import com.bawp.babyneeds.model.Diary;
  import com.bawp.babyneeds.R;
  import com.bawp.babyneeds.util.Util;
  
  import java.text.DateFormat;
  import java.util.ArrayList;
  import java.util.Date;
  import java.util.List;
  
  public class DatabaseHandlerDiary extends SQLiteOpenHelper {
  
      public DatabaseHandlerDiary(Context context) {
          super(context, Util.DATABASE_NAME_DIARY, null, Util.DATA_BASE_VERSION_DIARY);
      }
  
      @Override
      public void onCreate(SQLiteDatabase db) {
          String CREATE_DIARY_TABLE = "CREATE TABLE " + Util.TABLE_NAME_DIARY + " ("
                  + Util.KEY_ID_DIARY + " INTEGER PRIMARY KEY," + Util.KEY_NAME_DIARY + " TEXT,"
                  + Util.KEY_DESC_DIARY + " TEXT," + Util.KEY_DATE_ADDED_DIARY + " LONG);";
          db.execSQL(CREATE_DIARY_TABLE);
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          String DROP_TABLE = String.valueOf(R.string.db_drop);
          db.execSQL(DROP_TABLE, new String[]{Util.DATABASE_NAME_DIARY});
  
          //create new table
          onCreate(db);
      }
  
      //CRUD: create read update delete
  
      //add diary
      public void addDiary(Diary diary) {
          SQLiteDatabase db = this.getWritableDatabase();
  
          ContentValues values = new ContentValues();
          values.put(Util.KEY_NAME_DIARY, diary.getDiaryName());
          values.put(Util.KEY_DESC_DIARY, diary.getDiaryDesc());
          values.put(Util.KEY_DATE_ADDED_DIARY, java.lang.System.currentTimeMillis());
  
          //^ insert to row
          db.insert(Util.TABLE_NAME_DIARY, null, values);
          db.close();
      }
  
      //get diary
      public Diary getDiary(int id) {
          SQLiteDatabase db = this.getReadableDatabase();
  
          Cursor cursor = db.query(Util.TABLE_NAME_DIARY, new String[]{
                          Util.KEY_NAME_DIARY, Util.KEY_DESC_DIARY, Util.KEY_DATE_ADDED_DIARY},
                  Util.KEY_ID_DIARY +"=?", new String[]{String.valueOf(id)},
                  null, null, null);
  
          if (cursor != null) {
              cursor.moveToFirst();
          }
  
          Diary diary = new Diary();
          diary.setDiaryId(Integer.parseInt(cursor.getString(0)));
          diary.setDiaryName(cursor.getString(1));
          diary.setDiaryDesc(cursor.getString(2));
  
          return diary;
      }
  
      //get all diaries
      public List&lt;Diary&gt; getAllDiaries() {
          SQLiteDatabase db = this.getReadableDatabase();
  
          List&lt;Diary&gt; diaryList = new ArrayList&lt;&gt;();
  
          Cursor cursor = db.query(Util.TABLE_NAME_DIARY,
                  new String[]{Util.KEY_ID_DIARY,
                          Util.KEY_NAME_DIARY,
                          Util.KEY_DESC_DIARY,
                          Util.KEY_DATE_ADDED_DIARY},
                  null, null, null, null,
                  Util.KEY_DATE_ADDED_DIARY + " DESC");
  
          if (cursor.moveToFirst()) { //if item exists
              do {
                  Diary diary = new Diary();
                  diary.setDiaryId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(Util.KEY_ID_DIARY))));
                  diary.setDiaryName(cursor.getString(cursor.getColumnIndex(Util.KEY_NAME_DIARY)));
                  diary.setDiaryDesc(cursor.getString(cursor.getColumnIndex(Util.KEY_DESC_DIARY)));
  
                  //convert Timestamp to something readable
                  DateFormat dateFormat = DateFormat.getDateInstance();
                  String formattedDate = dateFormat.format(new Date(cursor.getLong(cursor.getColumnIndex(Util.KEY_DATE_ADDED_DIARY)))
                          .getTime()); // Feb 23, 2020
                  diary.setDateDiaryAdded(formattedDate);
  
                  //Add to arraylist
                  diaryList.add(diary);
              } while (cursor.moveToNext());
          }
          return diaryList;
      }
  
      public int updateItem(Diary diary) {
          SQLiteDatabase db = this.getWritableDatabase();
  
          ContentValues values = new ContentValues();
          values.put(Util.KEY_NAME_DIARY, diary.getDiaryName());
          values.put(Util.KEY_DESC_DIARY, diary.getDiaryDesc());
          values.put(Util.KEY_DATE_ADDED_DIARY, java.lang.System.currentTimeMillis());//timestamp of the system
  
          //update row
          return db.update(Util.TABLE_NAME_DIARY, values,
                  Util.KEY_ID_DIARY + "=?",
                  new String[]{String.valueOf(diary.getDiaryId())});
  
      }
  
      //Todo: Add Delete Item
      public void deleteItem(int id) {
          SQLiteDatabase db = this.getWritableDatabase();
          db.delete(Util.TABLE_NAME_DIARY,
                  Util.KEY_ID_DIARY + "=?",
                  new String[]{String.valueOf(id)});
  
          //close
          db.close();
  
      }
  
      //Todo: getItemCount
      public int getItemsCount() {
          String countQuery = "SELECT * FROM " + Util.TABLE_NAME_DIARY;
          SQLiteDatabase db = this.getReadableDatabase();
  
          Cursor cursor = db.rawQuery(countQuery, null);
  
          return cursor.getCount();
  
      }
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>package com.bawp.babyneeds.data;
  
  import android.content.ContentValues;
  import android.content.Context;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteOpenHelper;
  import com.bawp.babyneeds.model.Diary;
  import com.bawp.babyneeds.R;
  import com.bawp.babyneeds.model.Plant;
  import com.bawp.babyneeds.util.PlantDatabase;
  import com.bawp.babyneeds.util.Util;
  
  import java.text.DateFormat;
  import java.util.ArrayList;
  import java.util.Date;
  import java.util.List;
  
  public class DatabaseHandlerPlant extends SQLiteOpenHelper {
  
      public DatabaseHandlerPlant(Context context) {
          super(context, PlantDatabase.DATABASE_NAME_PLANT, null, PlantDatabase.DATA_BASE_VERSION_PLANT);
      }
  
      @Override
      public void onCreate(SQLiteDatabase db) {
          String CREATE_PLANT_TABLE = "CREATE TABLE " + PlantDatabase.TABLE_NAME_PLANT + " ("
                  + PlantDatabase.KEY_ID_PLANT + " INTEGER PRIMARY KEY," + PlantDatabase.KEY_NAME_PLANT + " TEXT,"
                  + PlantDatabase.KEY_PLANT_MEDIUM + " TEXT," + PlantDatabase.KEY_PLANT_POT_SIZE + " TEXT,"
                  + PlantDatabase.KEY_PLANT_WATTAGE + " TEXT," + PlantDatabase.KEY_PLANT_MISC_NOTES + " TEXT,"
                  + PlantDatabase.KEY_PLANT_SPECIES + " TEXT" + ")";
          db.execSQL(CREATE_PLANT_TABLE);
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          String DROP_TABLE = String.valueOf(R.string.db_drop);
          db.execSQL(DROP_TABLE, new String[]{PlantDatabase.DATABASE_NAME_PLANT});
  
          //create new table
          onCreate(db);
      }
  
      //CRUD: create read update delete
  
      //add diary
      public void addPlant(Plant plant) {
          SQLiteDatabase db = this.getWritableDatabase();
  
          ContentValues values = new ContentValues();
          values.put(PlantDatabase.KEY_NAME_PLANT, plant.getPlantName());
          values.put(PlantDatabase.KEY_PLANT_MEDIUM, plant.getPlantMedium());
          values.put(PlantDatabase.KEY_PLANT_POT_SIZE, plant.getPlantPotSize());
          values.put(PlantDatabase.KEY_PLANT_WATTAGE, plant.getPlantWattage());
          values.put(PlantDatabase.KEY_PLANT_MISC_NOTES, plant.getPlantDesc());
          values.put(PlantDatabase.KEY_PLANT_SPECIES, plant.getPlantSpecies());
  
          //^ insert to row
          db.insert(PlantDatabase.TABLE_NAME_PLANT, null, values);
          db.close();
      }
  
      //get plant
      public Plant getPlant(int id) {
          SQLiteDatabase db = this.getReadableDatabase();
  
          Cursor cursor = db.query(Util.TABLE_NAME_DIARY, new String[]{
                          Util.KEY_NAME_DIARY, Util.KEY_DESC_DIARY, Util.KEY_DATE_ADDED_DIARY},
                  Util.KEY_ID_DIARY +"=?", new String[]{String.valueOf(id)},
                  null, null, null);
  
          if (cursor != null) {
              cursor.moveToFirst();
          }
  
          Plant plant = new Plant();
          plant.setPlantId(Integer.parseInt(cursor.getString(0)));
          plant.setPlantName(cursor.getString(1));
          plant.setPlantMedium(cursor.getString(2));
          plant.setPlantPotSize(cursor.getString(3));
          plant.setPlantWattage(cursor.getString(4));
          plant.setPlantDesc(cursor.getString(5));
          plant.setPlantSpecies(cursor.getString(6));
  
          return plant;
      }
  
      //get all plants
      public List&lt;Plant&gt; getAllPlants() {
          SQLiteDatabase db = this.getReadableDatabase();
  
          List&lt;Plant&gt; plantList = new ArrayList&lt;&gt;();
  
          Cursor cursor = db.query(PlantDatabase.TABLE_NAME_PLANT,
                  new String[]{PlantDatabase.KEY_ID_PLANT,
                          PlantDatabase.KEY_NAME_PLANT,
                          PlantDatabase.KEY_PLANT_MEDIUM,
                          PlantDatabase.KEY_PLANT_POT_SIZE,
                          PlantDatabase.KEY_PLANT_WATTAGE,
                          PlantDatabase.KEY_PLANT_MISC_NOTES,
                          PlantDatabase.KEY_PLANT_SPECIES},
                  null, null, null,
                  null, null);
  
  
  
         /* Cursor cursor = db.query(Util.TABLE_NAME_DIARY,
                  new String[]{Util.KEY_ID_DIARY,
                          Util.KEY_NAME_DIARY,
                          Util.KEY_DESC_DIARY,
                          Util.KEY_DATE_ADDED_DIARY},
                  null, null, null, null,
                  Util.KEY_DATE_ADDED_DIARY + " DESC");*/
  
          if (cursor.moveToFirst()) { //if item exists
              do {
                  Plant plant = new Plant();
                  plant.setPlantId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(PlantDatabase.KEY_ID_PLANT))));
                  plant.setPlantName(cursor.getString(cursor.getColumnIndex(PlantDatabase.KEY_NAME_PLANT)));
                  plant.setPlantMedium(cursor.getString(cursor.getColumnIndex(PlantDatabase.KEY_PLANT_MEDIUM)));
                  plant.setPlantPotSize(cursor.getString(cursor.getColumnIndex(PlantDatabase.KEY_PLANT_POT_SIZE)));
                  plant.setPlantWattage(cursor.getString(cursor.getColumnIndex(PlantDatabase.KEY_PLANT_WATTAGE)));
                  plant.setPlantDesc(cursor.getString(cursor.getColumnIndex(PlantDatabase.KEY_PLANT_MISC_NOTES)));
                  plant.setPlantSpecies(cursor.getString(cursor.getColumnIndex(PlantDatabase.KEY_PLANT_SPECIES)));
  
                  /*//convert Timestamp to something readable
                  DateFormat dateFormat = DateFormat.getDateInstance();
                  String formattedDate = dateFormat.format(new Date(cursor.getLong(cursor.getColumnIndex(Util.KEY_DATE_ADDED_DIARY)))
                          .getTime()); // Feb 23, 2020
                  diary.setDateDiaryAdded(formattedDate);*/
  
                  //Add to arraylist
                  plantList.add(plant);
              } while (cursor.moveToNext());
          }
          return plantList;
      }
  
      public int updateItem(Plant plant) {
          SQLiteDatabase db = this.getWritableDatabase();
  
          ContentValues values = new ContentValues();
          values.put(PlantDatabase.KEY_NAME_PLANT, plant.getPlantName());
          values.put(PlantDatabase.KEY_PLANT_MEDIUM, plant.getPlantMedium());
          values.put(PlantDatabase.KEY_PLANT_POT_SIZE, plant.getPlantPotSize());
          values.put(PlantDatabase.KEY_PLANT_WATTAGE, plant.getPlantWattage());
          values.put(PlantDatabase.KEY_PLANT_MISC_NOTES, plant.getPlantDesc());
          values.put(PlantDatabase.KEY_PLANT_SPECIES, plant.getPlantSpecies());
  
          //update row
          return db.update(PlantDatabase.TABLE_NAME_PLANT, values,
                  PlantDatabase.KEY_ID_PLANT + "=?",
                  new String[]{String.valueOf(plant.getPlantId())});
  
      }
  
      //Todo: Add Delete Item
      public void deleteItem(int id) {
          SQLiteDatabase db = this.getWritableDatabase();
          db.delete(PlantDatabase.TABLE_NAME_PLANT,
                  PlantDatabase.KEY_ID_PLANT + "=?",
                  new String[]{String.valueOf(id)});
  
          //close
          db.close();
  
      }
  
      //Todo: getItemCount
      public int getItemsCount() {
          String countQuery = "SELECT * FROM " + PlantDatabase.TABLE_NAME_PLANT;
          SQLiteDatabase db = this.getReadableDatabase();
  
          Cursor cursor = db.rawQuery(countQuery, null);
  
          return cursor.getCount();
  
      }
  
  }
  </code></pre>