Room persistance library. Delete all

前端 未结 8 1112
再見小時候
再見小時候 2020-12-04 11:59

How can I delete all entries on specific table using Room Persistence Library? I need to drop table, but I cannot to find any information how to do this.

Only when d

相关标签:
8条回答
  • 2020-12-04 12:04

    Combining what Dick Lucas says and adding a reset autoincremental from other StackOverFlow posts, i think this can work:

        fun clearAndResetAllTables(): Boolean {
            val db = db ?: 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()
            }
        }
    
    0 讨论(0)
  • 2020-12-04 12:06

    As of Room 1.1.0 you can use clearAllTables() which:

    Deletes all rows from all the tables that are registered to this database as entities().

    0 讨论(0)
  • 2020-12-04 12:09

    Here is how I have done it in Kotlin.

    1. Inject room db in the activity using DI (Koin).

       private val appDB: AppDB by inject()
      
    2. Then you can simply call clearAllTables()

    private fun clearRoomDB() {
        GlobalScope.launch {
            appDB.clearAllTables()
            preferences.put(PreferenceConstants.IS_UPLOADCATEGORIES_SAVED_TO_DB, false)
            preferences.put(PreferenceConstants.IS_MEMBERHANDBOOK_SAVED_TO_DB, false)
        }
    }
    
    0 讨论(0)
  • 2020-12-04 12:11

    If want to delete an entry from the the table in Room simply call this function,

    @Dao
    public interface myDao{
        @Delete
        void delete(MyModel model);
    }
    

    Update: And if you want to delete complete table, call below function,

      @Query("DELETE FROM MyModel")
      void delete();
    

    Note: Here MyModel is a Table Name.

    0 讨论(0)
  • 2020-12-04 12:15

    Use clearAllTables() with RXJava like below inorder to avoid java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.

    Completable.fromAction(new Action() {
            @Override
            public void run() throws Exception {
                getRoomDatabase().clearAllTables();
            }
        }).subscribeOn(getSchedulerProvider().io())
                .observeOn(getSchedulerProvider().ui())
                .subscribe(new Action() {
                    @Override
                    public void run() throws Exception {
                        Log.d(TAG, "--- clearAllTables(): run() ---");
                        getInteractor().setUserAsLoggedOut();
                        getMvpView().openLoginActivity();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        Log.d(TAG, "--- clearAllTables(): accept(Throwable throwable) ----");
                        Log.d(TAG, "throwable.getMessage(): "+throwable.getMessage());
    
    
                    }
                });
    
    0 讨论(0)
  • 2020-12-04 12:25

    I had issues with delete all method when using RxJava to execute this task on background. This is how I finally solved it:

    @Dao
    interface UserDao {
        @Query("DELETE FROM User")
        fun deleteAll()
    }
    

    and

    fun deleteAllUsers() {
        return Maybe.fromAction(userDao::deleteAll)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe ({
                d("database rows cleared: $it")
            }, {
                e(it)
            }).addTo(compositeDisposable)
    }
    
    0 讨论(0)
提交回复
热议问题