Android Room-如何清除所有表的sqlite_sequence [英] Android room - How to clear sqlite_sequence for all table
问题描述
如果用户从应用程序注销,我将使用一张一张地清除表中的数据
If user logout from the app, i'm clearing data from tables one by one using
@Query("DELETE FROM tableName")
然后,我尝试使用以下代码一张一张地清除所有表的sqlite_sequence.
Then i'm trying to clear the sqlite_sequence for all table one by one using below code.
database = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, DatabaseMeta.DB_NAME)
.build();
database.query("DELETE FROM sqlite_sequence WHERE name = ?", new Object[]{"tableName"})
很遗憾,清除 sqlite_sequence
无效.因此,如果用户再次登录,则不会从1创建起始rowId.
Unfortunately clearing sqlite_sequence
is not working. So if the user login again, then the starting rowId is not created from 1.
还有其他方法可以做到吗?我试图清除整个数据库,并在用户再次登录后添加新条目.
Is there any other way to do this? I trying to clear whole DB and will add the new entries once the user login again.
推荐答案
看来,无论是通过DAO还是通过原始查询,Room数据库都不支持编辑sqlite_sequence表.相反,这是我解决此问题的方法(科特琳):
It appears Room Database doesn't support editing the sqlite_sequence table, either through a DAO or through a raw query. Instead, here's how I worked around this problem (Kotlin):
class NonRoomDb(context:Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION) {
override fun onCreate(db: SQLiteDatabase?) {}
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {}
}
fun resetPointer(context:Context, tableName:String) {
val nonRoomDb = NonRoomDb(context)
nonRoomDb.writableDatabase.execSQL("DELETE FROM sqlite_sequence WHERE name='$tableName';")
nonRoomDb.close()
}
这篇关于Android Room-如何清除所有表的sqlite_sequence的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!