0

I want to make a WHERE query in android without naming the specific field name. namely, I want to make a WHERE query without defining the field name (it will determine at runtime).

I tried to do something like this:

@Query("SELECT COUNT(*) FROM Workspace WHERE :name = :value")
int countFieldsFor(String name, String value)

But it's not working...

How can I implement this? Even by querying directly to the database (without room)

Thanks.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Roi Amiel
  • 353
  • 1
  • 3
  • 18

1 Answers1

1

You can use @RawQuery and simply pass generated SQL to dao interface method:

@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);
Ahmet Zorer
  • 122
  • 1
  • 8