gpt4 book ai didi

android - 尝试从 Room 数据库实例中检索值时抛出 IllegalStateException

转载 作者:行者123 更新时间:2023-11-29 19:06:03 25 4
gpt4 key购买 nike

我正在使用 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()
}
}

这是我调用这个方法的代码

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"

最佳答案

我解决了我的问题,实际问题,在 SQLite 表行大小中,当我试图从表中获取数据时,该行包含超过 2.5 MB 的数据并且在 SQLite 数据库中不是很好,比我通过拆分字节数组并按 block 保存它来减少数据的大小,之后每件事都完美地工作,在获取之后我将它合并为原​​始数据

关于android - 尝试从 Room 数据库实例中检索值时抛出 IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47203082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com