简体   繁体   中英

(Room Database)fields which are not returned by the query error

Here is my code:

ClassEntity.java

@Entity

public class ClassEntity {

    @NonNull
    @PrimaryKey
    public String id_of_a_group;
    public String monday;
    public String tuesday;
    public String wednesday;
    public String thursday;    
    public String friday;
}

Dao.java

@androidx.room.Dao    
public interface Dao {

    @Query("SELECT monday FROM  ClassEntity WHERE id_of_a_group = :id")
    List<ClassEntity> findFromMonday(String id);

    @Query("SELECT tuesday FROM  ClassEntity WHERE id_of_a_group = :id")
    List<ClassEntity> findFromTuesday(String id);

    @Query("SELECT wednesday FROM  ClassEntity WHERE id_of_a_group = :id")   
    List<ClassEntity> findFromWednesday(String id);

    @Query("SELECT thursday FROM  ClassEntity WHERE id_of_a_group = :id")
    List<ClassEntity> findFromThursday(String id);

    @Query("SELECT friday FROM  ClassEntity WHERE id_of_a_group = :id")
    List<ClassEntity> findFromFriday(String id);

    @Query("SELECT id_of_a_group FROM  ClassEntity")
    List<ClassEntity> getIdOfAllGroups();

    @Insert
    void insert(Class group);

    @Update
    void update(Class group);

    @Delete
    void delete(Class group);
}

Database.java

@androidx.room.Database(entities = {ClassEntity.class},version=1)

public abstract class Database extends RoomDatabase {

    public abstract Dao classDao();   
    public abstract Array[] getIdOfAllGroups();
}

App.java

public class App extends Application {

    public static App instance;    
    private Database database;

    @Override
    public void onCreate() {

        super.onCreate();
        instance = this;
        database = Room.databaseBuilder(this, Database.class, "database")
                .build();
    }

    public static App getInstance() {
        return instance;
    }

    public Database getDatabase() {
        return database;
    }
}

The compiler gives me this error:

ClassEntity has some fields [id_of_a_group, tuesday, wednesday, thursday, friday] which are not returned by the query. If they are not supposed to be read from the result, you can mark them with @Ignore annotation. You can suppress this warning by annotating the method with @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH). Columns returned by the query: monday. Fields in com.example.schedule.ClassEntity: id_of_a_group, monday, tuesday, wednesday, thursday, friday.

I do not know the solution cause I am a newbie. Btw, I need to check if there is the item in the database.

Here's my guess of doing this, just tell me if I'm wrong or not

    Others2 activity=(Others2)getActivity();
    getData=activity.sendData();
    Database db = App.getInstance().getDatabase();
    String[] ids_of_all_groups=db.getIdOfAllGroups();
    boolean test=false;

    for( String elem: ids_of_all_groups){
        if(getData==elem){
            test=true;
            break;
        }
    }

You are trying to tell Room to build a ClassEntity object from a single String (for each query). It is saying that it cannot safely do this.

The fix is to return a List<String> as oppsed to a List<calssEntity> .

You are also trying to insert/delete/update using a Class object when you want to insert/update/delete based upon a ClassEntity object. So your Dao should/could be :-

@androidx.room.Dao
public interface Dao {

    @Query("SELECT monday FROM  ClassEntity WHERE id_of_a_group = :id")
    List<String> findFromMonday(String id);

    @Query("SELECT tuesday FROM  ClassEntity WHERE id_of_a_group = :id")
    List<String> findFromTuesday(String id);

    @Query("SELECT wednesday FROM  ClassEntity WHERE id_of_a_group = :id")
    List<String> findFromWednesday(String id);

    @Query("SELECT thursday FROM  ClassEntity WHERE id_of_a_group = :id")
    List<String> findFromThursday(String id);

    @Query("SELECT friday FROM  ClassEntity WHERE id_of_a_group = :id")
    List<String> findFromFriday(String id);

    @Query("SELECT id_of_a_group FROM  ClassEntity")
    List<String> getIdOfAllGroups();

    @Insert
    void insert(ClassEntity group);

    @Update
    void update(ClassEntity group);

    @Delete
    void delete(ClassEntity group);
}

You should also comment out or remove the line (in Database.java)

public abstract Array[] getIdOfAllGroups();

I believe that the above changes will allow the compilation.

As for checking items in the database you can add a query to check if there are any rows in the ClassEntity table by adding the following to Dao.java

@Query("SELECT count() FROM ClassEntity")
int getItemsInDatabase();

using the above changes and then the following in an activity :-

    database = Room.databaseBuilder(this, Database.class, "database")
            .allowMainThreadQueries()
            .build();
    Log.d("ITEMSINDB",String.valueOf(database.classDao().getItemsInDatabase()));
    if (database.classDao().getItemsInDatabase() < 1) {
        ClassEntity myClassEntity = new ClassEntity();
        myClassEntity.id_of_a_group = "myfirstid";
        myClassEntity.monday = "something on Monday";
        myClassEntity.tuesday = "on Tuesday";
        myClassEntity.wednesday = "another on Wed";
        myClassEntity.thursday = "this on Thurs";
        myClassEntity.friday = "TGIF";
        database.classDao().insert(myClassEntity);
    }
    Log.d("ITEMSINDB",String.valueOf(database.classDao().getItemsInDatabase()));

results in :-

 2020-01-07 09:48:30.251 D/ITEMSINDB: 0 2020-01-07 09:48:30.254 D/ITEMSINDB: 1

If rerun then as data exists the result is (row already exists and therefore no new row added)

 2020-01-07 09:57:03.217 D/ITEMSINDB: 1 2020-01-07 09:57:03.218 D/ITEMSINDB: 1

regarding public abstract Array[] getIdOfAllGroups(); you would use (in the App activity) :-

List<String> mygroups = database.classDao().getIdOfAllGroups();

If you wanted to check if a group exists according to it's id_of_a_group then you could have

@Query("SELECT count() > 0  FROM ClassEntity WHERE id_of_a_group = :id")
boolean doesGroupExist(String id);

The you could use something like :-

    String test1 = "this group doesn not exists";
    if (!database.classDao().doesGroupExist(test1)) {
        Log.d("GROUPLOOKUPRESULT","Group " + test1 + " Not located.");
    } else {
        Log.d("GROUPLOOKUPRESULT","Group " + test1 + " Located.");
    }
    String test2 = "myfirstid";
    if (database.classDao().doesGroupExist(test2)) {
        Log.d("GROUPLOOKUPRESULT","Group " + test2 + " Located.");
    } else {
        Log.d("GROUPLOOKUPRESULT","Group " + test2 + " Not Located.");
    }

This results in :-

 2020-01-07 10:17:49.201 D/GROUPLOOKUPRESULT: Group this group doesn not exists Not located. 2020-01-07 10:17:49.201 D/GROUPLOOKUPRESULT: Group myfirstid Located.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM