Is it possible to fetch data by passing the table name as a parameter? Something like this.
@Query("SELECT id, name from :tableName")
fun getData(tableName: String): List<RandomModel>
Try this
@Dao
interface RawDao {
@RawQuery
List<RandomModel> getData(SupportSQLiteQuery query);
}
SimpleSQLiteQuery query = new SimpleSQLiteQuery("SELECT * FROM "+ tablename);
List<RandomModel> models = rawDao.getUserViaQuery(query);
I think Room does not support dynamic tableName.
We have two ways:
1-In DAO, we can replace tableName with the actual table name, as defined on the model @Entity
2-We can use @RawQuery like this :
@Dao
interface RawDao {
@RawQuery
User getUserViaQuery(SupportSQLiteQuery query);
}
SimpleSQLiteQuery query = new SimpleSQLiteQuery("SELECT * FROM User WHERE id = ? LIMIT 1",
new Object[]{userId});
User user2 = rawDao.getUserViaQuery(query);
You can study more at this
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With