我使用android新架构组件 room 进行数据库操作,当我尝试从数据库中检索对象时遇到了麻烦
Couldn't read row 0, col 0 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
这是我的Dao界面
@Dao
interface BookDao {
@Query("SELECT * FROM books")
fun getAllBooks() : List<Book>
@Insert
fun saveBook(book: Book)
@Query("DELETE FROM books")
fun deleteBookTable()
}
这是我的表模型类
@Entity(tableName = "books")
data class Book(
@ColumnInfo(name = "book_title")
var title: String,
var imgRes: Int,
var filePath: String? = null,
var pdfInBytes: ByteArray? = null,
@PrimaryKey
@ColumnInfo(name = "_id")
var bookId: String = UUID.randomUUID().toString()
) : Serializable
这是RoomDatabase子类
@Database(entities = arrayOf(Book::class), version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun bookDao(): BookDao
companion object {
@Volatile private var INSTANCE: AppDatabase? = null
fun getInstance(context: Context): AppDatabase =
INSTANCE ?: synchronized(this) {
INSTANCE ?: buildDatabase(context).also { INSTANCE = it }
}
private fun buildDatabase(context: Context) =
Room.databaseBuilder(context.applicationContext,
AppDatabase::class.java, "booksDB.db")
.build()
}
}
这是我调用此方法的代码bloke
private val db: AppDatabase by lazy { AppDatabase.getInstance(this) }
override fun onCreate(savedInstanceState: Bundle?) {
doAsync { // from anko library
for (i in 0..10){
val book = Book("$i", tmpImgRes, "tmpPath", tmpBytes)
db.bookDao().saveBook(book)
}
val books = db.bookDao().getAllBooks()
}
}
当我试图调用抛出的getAllBooks()异常时。
房间实施:
implementation "android.arch.persistence.room:runtime:1.0.0-rc1"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0-rc1"
答案 0 :(得分:2)
我解决了我的问题,实际的问题,在SQLite表行的大小,当我试图从表中获取数据时,包含超过2.5 MB数据的行,并且它在SQLite数据库中不是很好,我通过拆分字节数组来减少数据的大小并通过块保存它,之后每个东西都完美地工作,在获取之后我将它合并为原始的