sqlite 如何使用io.requery & Room在Android应用程序中执行PRAGMA语句

sgtfey8w  于 2022-12-23  发布在  SQLite
关注(0)|答案(1)|浏览(151)

我正在调查Sqlite PRAGMA语句在我当前的Android应用程序上的性能改进。
我将此代码用作RoomDatabase.CallbackonCreate函数

@Provides
@Singleton
fun providesRoomDatabaseCallback(@ApplicationContext appContext: Context): RoomDatabase.Callback {
    return object : RoomDatabase.Callback() {
        override fun onCreate(db: SupportSQLiteDatabase) {
            super.onCreate(db)
            db.execSQL("PRAGMA temp_store=2;")
            db.execSQL("PRAGMA journal_mode=MEMORY")
            db.execSQL("PRAGMA auto_vacuum=incremental;")
        }
    }
}

然而,当我的应用程序第一次启动时,我在logcat中看到此崩溃:-

FATAL EXCEPTION: main
Process: com.my.app, PID: 30620
  android.database.sqlite.SQLiteException: unknown error (code 0): Queries can be performed using SQLiteDatabase query or rawQuery methods only.
    at io.requery.android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)
    at io.requery.android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:778)
    at io.requery.android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:764)
    at io.requery.android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:71)
    at io.requery.android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1923)
    at io.requery.android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1864)
    at com.my.app.background.database.hilt.DatabaseModule$providesRoomDatabaseCallback$1.onCreate(DatabaseModule.kt:33)

令人不快的PRAGMA语句如下所示

db.execSQL("PRAGMA journal_mode=MEMORY")

如何在Android应用程序中配置sqlite journal_mode?
我错过了什么?

z2acfund

z2acfund1#

切换到query修复了它

db.query("PRAGMA temp_store=2;")
db.query("PRAGMA journal_mode=memory;")
db.query("PRAGMA auto_vacuum=incremental;")

相关问题