会议室数据库查询返回空列表

时间:2018-07-07 13:10:33

标签: android sqlite android-room

我正在一个使用Room数据库存储和检索对象的项目中。我已经使用RoomDB,Dao和Entity注释正确设置了所有内容。

当我尝试检索以前保存的对象时,结果得到一个空列表。

这是我检索项目的方法:

class SpotRepository(context: Context) {

    private val itemDAO: SpotDAO = RoomDB.getDB(context).spotDao



    val allSpotItemsList: List<Spot>
        get() = itemDAO.getAllSpotItemsList()
 }

这是我的物品道

@Dao abstract class SpotDAO{

    @Query("SELECT * FROM spotItems")
    abstract fun getAllSpotItems(): LiveData<List<Spot>>

    @Query("SELECT * FROM spotItems")
    abstract fun getAllSpotItemsList(): List<Spot>


    @Query("SELECT * FROM spotItems WHERE uuid = :id")
    abstract fun getSpotById(id: String): Spot

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    abstract fun insertItem(item: Spot): Long

    @Query("DELETE FROM spotItems WHERE id = :id")
    abstract fun deleteItem(id: Long)


    @Update
    abstract fun update(item: Spot): Int }

这是我的RoomDB

@Database(entities = {Spot.class}, version = 1)
@TypeConverters({Converters.class})
public abstract class RoomDB extends RoomDatabase {

    private static RoomDB INSTANCE;
    private final static Object lock = new Object();
    public abstract SpotDAO getSpotDao();
    public static RoomDB getDB(Context context){
        synchronized (lock){
            if(INSTANCE == null){
                INSTANCE = Room.databaseBuilder(context, RoomDB.class, "Items.db")
                        .build();
            }
            return INSTANCE;
        }
    }




}

这是我的实体

@Entity(tableName = "spotItems") @JsonIgnoreProperties("photo", "id") class Spot : Serializable{


    @PrimaryKey(autoGenerate = true)
    var id: Long = 0
    @ColumnInfo(name = "hidden")
    var hidden = false
    @ColumnInfo(name = "address")
    var address = ""
    @ColumnInfo(name = "uuid")
    var uuid = ""
    @ColumnInfo(name = "latitude")
    var latitude: Double = 0.0
    @ColumnInfo(name = "longitude")
    var longitude: Double = 0.0
    @ColumnInfo(name = "message")
    var message = ""
    @ColumnInfo(name = "picture_url")
    var picture_url = ""
    @ColumnInfo(name = "thumb_url")
    var thumb_url = ""
    @ColumnInfo(name = "comment_count")
    var comment_count = 0
    @ColumnInfo(name = "poster_uuid")
    var poster_uuid = "poster_uuid_place_holder"
    @Ignore
    var likes = HashMap<String, Boolean>()
    @Ignore
    var dislikes = HashMap<String, Boolean>()
    @ColumnInfo(name = "timestamp")
    var timestamp = 0L

    //  This needs to only get used for reuploading!!!
    @ColumnInfo(name = "photo")
    var photo: Bitmap? = null

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as Spot

        if (uuid != other.uuid) return false

        return true
    }

    override fun hashCode(): Int {
        return uuid.hashCode()
    }


}

当我尝试访问通过使用保存的项目列表时,会出现我的问题

  val spotsForReupload = SpotRepository(this@MainActivity).allSpotItemsList

                    Log.e("NUMBER", spotsForReupload.size.toString())

在这里,检索到的项目数始终为0

我从中得到的唯一有用的提示是控制台中的以下错误日志

  

E / SQLiteCursor:onMove()返回false。 RequiredPos:0 WindowStartPos:   0 WindowRowCount:0(原始查询数:1)

我不知道此错误来自何处,也不知道这意味着什么

为什么我会得到一个空名单?谢谢

1 个答案:

答案 0 :(得分:1)

编辑:我做了一些难以置信的蠢事。我试图以字节数组的形式在数据库中保存10mb的位图。别那样