gpt4 book ai didi

android - 房间livedata : crash on launch

转载 作者:行者123 更新时间:2023-12-05 00:01:44 28 4
gpt4 key购买 nike

最近我决定开始使用LiveData在我的 Room 数据库中,将其添加到 ado 方法中,然后在 ViewModel 中观察它们,然后即使我在进行所有更改并进行迁移后更新了数据库版本,它也会在模拟器启动时引发此错误

2022-07-03 15:35:17.723 4866-4891/com.example... E/SQLiteLog: (1) statement aborts at 1: [ROLLBACK;] cannot rollback - no transaction is active
2022-07-03 15:35:17.724 4866-4891/com.example... E/AndroidRuntime: FATAL EXCEPTION: arch_disk_io_0
Process: com.example..., PID: 4866
java.lang.RuntimeException: Exception while computing database live data.
at androidx.room.RoomTrackingLiveData$1.run(RoomTrackingLiveData.java:92)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:920)
Caused by: android.database.sqlite.SQLiteException: cannot rollback - no transaction is active (code 1 SQLITE_ERROR)
at android.database.sqlite.SQLiteConnection.nativeExecute(Native Method)
at android.database.sqlite.SQLiteConnection.execute(SQLiteConnection.java:709)
at android.database.sqlite.SQLiteSession.endTransactionUnchecked(SQLiteSession.java:441)
at android.database.sqlite.SQLiteSession.endTransaction(SQLiteSession.java:403)
at android.database.sqlite.SQLiteDatabase.endTransaction(SQLiteDatabase.java:589)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:422)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:316)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:151)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:112)
at androidx.room.RoomDatabase.inTransaction(RoomDatabase.java:706)
at androidx.room.RoomDatabase.assertNotSuspendingTransaction(RoomDatabase.java:483)
at androidx.room.RoomDatabase.query(RoomDatabase.java:526)
at androidx.room.util.DBUtil.query(DBUtil.java:86)
at com.example...data.database.NoteDao_Impl$16.call(NoteDao_Impl.java:723)
at com.example...data.database.NoteDao_Impl$16.call(NoteDao_Impl.java:720)
at androidx.room.RoomTrackingLiveData$1.run(RoomTrackingLiveData.java:90)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
at java.lang.Thread.run(Thread.java:920) 
我的道代码:
@Dao
interface NoteDao {

@Delete
fun deleteFolder(folder: Folder)

@Query("UPDATE Note SET isStarred = :state WHERE id = :id")
fun updateNoteChecked(id : Int, state : Boolean)

@Query("DELETE FROM Note WHERE id = :id")
fun deleteNoteById(id : Int)

@Query("SELECT * FROM Note WHERE id = :id")
fun getNoteById(id : Int) : Note

@Query("SELECT title FROM Folder WHERE id = :id")
fun getFolderTitleById(id : Int) : String

@Query("SELECT * FROM folder WHERE id = :id")
fun getFolderById(id : Int) : Folder

@Query("UPDATE Folder SET title = :title WHERE id = :id")
fun updateFolderTitle(id : Int, title : String)

@Query("SELECT * FROM Folder WHERE id = :id")
fun getFolderWithNotesByFolderId(id : Int) : FolderWithNotes

@Query("SELECT * FROM Note WHERE folderId = :id ORDER BY isStarred")
fun getNotesByFolderId(id : Int) : LiveData<List<Note>>

@Query("SELECT * FROM note WHERE content LIKE :query ORDER BY isStarred")
fun searchAllNotes(query : String?) : LiveData<List<Note>>

@Query("SELECT * FROM note WHERE folderId = :id AND content LIKE :query ORDER BY isStarred" )
fun searchNotesByFolderId(query : String?, id : Int) : LiveData<List<Note>>

@Query("SELECT * FROM Note ORDER BY isStarred")
fun getAllNotes() : LiveData<List<Note>>

@Query("SELECT * FROM Folder WHERE id = :id")
fun getFolderWithNotes(id : Int) : List<FolderWithNotes>

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertFolder(folder : Folder)

@Query("SELECT * FROM Folder")
fun getAllFolders() : LiveData<List<Folder>>

@Query("UPDATE folder SET noteCount = noteCount + 1 WHERE id = :id")
fun updateOnInsertNote(id : Int)

@Query("UPDATE folder SET noteCount = noteCount - 1 WHERE id = :id")
fun updateOnDeleteNote(id : Int)

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertNote(note : Note)

@Delete
suspend fun deleteNote(note : Note)
}
数据库类:
@Database(entities = [Note::class, Folder::class] , version =  11 )
abstract class NoteDatabase : RoomDatabase() {
abstract fun NoteDao() : NoteDao
}
数据库刀柄提供方法:
@Provides
@Singleton
fun provideNoteDatabase(app : Application) : NoteDatabase{
return Room.databaseBuilder(
app ,
NoteDatabase::class.java,
"notes",

).fallbackToDestructiveMigration()
.addCallback(DatabaseCallback())
.build()
}

最佳答案

根据我的评论,我怀疑问题出在回调 (DatabaseCallback) 中,并且可能在覆盖的 onOpen 中。方法。
原因是跟踪显示:-

  • 您已根据 at androidx.room.RoomDatabase.query(RoomDatabase.java:526) 发出查询
  • 根据 at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:316) 成功打开数据库
    - 然后似乎结束事务(不提交事务)。正如 Room 被证明的那样,那么最可能的问题是在没有事务开始的情况下尝试结束事务,因此,由于缺少提交,因此无处/无回滚。
  • 关于android - 房间livedata : crash on launch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72845512/

    28 4 0