会议室:数据库调用检索的null

时间:2018-08-06 07:15:00

标签: android recursion android-room

我试图从应用程序的许多不同区域调用数据库,但是在保存和检索对象时遇到了麻烦。当我检查数据时,看起来好像在为正确的列获取光标,但是它似乎 总是返回空对象 ,即使这些列中似乎有数据

cursor columns with data

我不确定问题是什么,但可能与对变量浏览器中发现的数据库的递归查找调用有关(就我关心的探索而言,它将继续这种级联)。 recursive Dao

要通过Android Room访问我的SQLite数据库,我可以从任何相关类中使用诸如下面的方法调用(此方法从各种唯一对象中多次调用过[我应该关闭它吗?]):

@Entity(tableName = "cell")
public class LivingCell extends AsyncTask<Integer, Integer, Integer>{
    ....
    getCellsDB.getCell(uid);}

public CellsDBHandler getCellsDB(){
    if (cellsDBHandler == null)
        cellsDBHandler = CellsDBHandler.getAppDatabase(MainActivity.getContext(),uid);
        return cellsDBHandler;
}

现在,我的印象是它不会创建数据库处理程序的新实例,因为这是我的CellsDBHandler的样子:

@Database(entities = {LivingCell.class})
public abstract class CellsDBHandler  extends RoomDatabase {
    private static CellsDBHandler INSTANCE;
    public abstract CellDao cellDao();
    public static CellsDBHandler getAppDatabase(Context context, String cityUid) {
        if (INSTANCE == null) {
        INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                                        CellsDBHandler.class,
                                        DBPath).build();}
        return INSTANCE;
}

    public LivingCell getCell(String cellUid) {
        return cellDao().getCellByID(cellUid);}
}

还有我的CellDao

//ALL DAO CALLS ARE RETURNING NULL OBJECTS BUT HAVE POPULATED TABLES
@Dao 
public interface CellDao {
    @Query("SELECT * FROM cell")
    List<LivingCell> getAll();

    @Query("SELECT uid FROM cell")
    List<String> getAllUids();

    @Query("SELECT * FROM cell WHERE uid LIKE (:cellUid)")
    LivingCell getCellByID(String cellUid);


    @Insert(onConflict = REPLACE)
    void insert(LivingCell cell);
...

也许我对MainActivity的调用是问题所在……这完全是找到应用程序上下文的可接受方法吗?

private static MainActivity instance;
public static Context getContext(){
    return instance;
}

万一重要,我所有的UID都是:

//form of: 1c21df2a-5b8f-4a56-ac5b-ed385ba8e4b8
uid = UUID.randomUUID().toString();

1 个答案:

答案 0 :(得分:0)

由于cellUid是单个字符串,而不是数组或集合,请尝试以下操作

@Query("SELECT * FROM cell WHERE uid LIKE :cellUid")
相关问题