Android Room - How to reset auto generated table primary key on each app run

前端 未结 4 1763
南方客
南方客 2020-12-06 08:02

I\'m using Room in order to persist data. I have a Entity that has a automatically generated (autoGenerate) primary key that mimics a ticket system. On every applica

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-06 08:46

    Taking as model what MikeT says.

    I think this can work:

            fun clearAndResetAllTables(): Boolean {
            if (db == null) return false
    
            // reset all auto-incrementalValues
            val query = SimpleSQLiteQuery("DELETE FROM sqlite_sequence")
    
            db!!.beginTransaction()
            return try {
                db!!.clearAllTables()
                db!!.query(query)
                db!!.setTransactionSuccessful()
                true
            } catch (e: Exception){
                false
            } finally {
                db!!.endTransaction()
            }
        }
    

提交回复
热议问题