Edward James
Edward James

Reputation: 131

SQLite Room Database: Receiving data from specific Column and Row not working?

I have a SQLite Room Database with 5 columns(Day, Likes, Dislikes, plays, Blocked). I have populated the database manually with 8 rows of data in the Database.

From the database I want to retrieve a specific data for a specific day. So I need number of likes (or Dislikes, plays,...) for day 6 (or 1,2,3,4...)

I tried to create the necessary Dao, Repository, and ViewModel methods, but they seems wrong because when I call it in MainActivity it gives me 0 as the return value.

Database Sample:

@Database(entities = Recovery.class, version = 1, exportSchema = false)
public abstract class RecoveryDatabase extends RoomDatabase {

private static RecoveryDatabase INSTANCE;
public abstract RecoveryDao recoveryDao();

// Singleton
public static RecoveryDatabase getInstance(final Context context) {
    if (INSTANCE == null) {
        synchronized (RecoveryDatabase.class) {
            if (INSTANCE == null) {
                INSTANCE = Room.databaseBuilder(context.getApplicationContext(), RecoveryDatabase.class, "recovery_database").fallbackToDestructiveMigration().allowMainThreadQueries().addCallback(roomCallback).build();
            }
        }
    }
    return INSTANCE;
}

private static RecoveryDatabase.Callback roomCallback = new RoomDatabase.Callback() {
    @Override
    public void onCreate(@NonNull SupportSQLiteDatabase db) {
        super.onCreate(db);
        new PopulateDbAsyncTask(INSTANCE).execute();
    }
};

private static class PopulateDbAsyncTask extends AsyncTask<Void, Void, Void> {
    private RecoveryDao recoveryDao;

    private PopulateDbAsyncTask(RecoveryDatabase db) {
        recoveryDao = db.recoveryDao();
    }

    @Override
    protected Void doInBackground(Void... voids) {
        recoveryDao.insert(new Recovery(1,22,9,2,3));
        recoveryDao.insert(new Recovery(2,23,8,3,4));
        recoveryDao.insert(new Recovery(3,56,6,4,5));
        recoveryDao.insert(new Recovery(4,34,4,2,2));
        recoveryDao.insert(new Recovery(5,76,2,7,9));
        recoveryDao.insert(new Recovery(6,10,1,4,7));
        recoveryDao.insert(new Recovery(7,27,6,7,3));
        recoveryDao.insert(new Recovery(8,46,7,1,5));
        return null;
    }
}

}

Upvotes: 1

Views: 1169

Answers (1)

sergiy tykhonov
sergiy tykhonov

Reputation: 5103

Try next options:

  1. Set column name likes explicitly in your DAO (it's not clear for Room what column you want use for value to return):
@Query("SELECT likes FROM recovery_table WHERE day=:day")
int getRecoveryWithDay(int day);
  1. Change type of value your DAO return to Recovery (it is better choice if you want to get several values - likes, dislikes, plays with one query):
@Query("SELECT * FROM recovery_table WHERE day=:day")
Recovery getRecoveryWithDay(int day);

You then can get likes in your Activity:

int day = RecoveryDatabase.getInstance(this).recoveryDao().getRecoveryWithDay(6).likes;

If neither of options work check if there is really row in your database with day = 6 with some sqlite browser

Upvotes: 2

Related Questions