ActiveAndroid icon indicating copy to clipboard operation
ActiveAndroid copied to clipboard

How to clear all data in DB?

Open rayboot opened this issue 13 years ago • 11 comments

Is there any method to clear all data??

rayboot avatar May 31 '13 02:05 rayboot

There is no method to do this built into AA. You can however, execute raw sql to do this.

SQLiteUtils.execSql("<your truncate sql here>");

pardom-zz avatar May 31 '13 13:05 pardom-zz

Within an activity: this.deleteDatabase("your_db");

rothschild86 avatar Oct 28 '13 03:10 rothschild86

@rothschild86 Does this require you to "restart" your app in order for the database to be regenerated?

joshuapinter avatar Feb 03 '15 20:02 joshuapinter

yeah, i have to restart. don't remember for what reason. if i remember, AA assumes that db still exists and crashes otherwise.

rothschild86 avatar Feb 03 '15 21:02 rothschild86

@rothschild86 Makes sense. If you're calling deleteDatabase() in an Activity, at that point all the ActiveAndroid setup would have been done so it'd be "surprised" if it vanishes.

Good tip, thanks!

joshuapinter avatar Feb 03 '15 21:02 joshuapinter

for sure. i have it hooked up to "Log out", so it's not really jarring to the user that the app closes.

rothschild86 avatar Feb 03 '15 21:02 rothschild86

@rothschild86 Yeah, good call. I'm using it in development just to test things out and clearing out the database after changes without having to change the database name, so that works.

joshuapinter avatar Feb 03 '15 21:02 joshuapinter

Deleting the database is vital when deauthenticating the user. AA should be able to handle this. looks like another pull request is in order.

bpappin avatar Mar 23 '16 20:03 bpappin

@bpappin Err.... AA does handle it. See https://github.com/pardom/ActiveAndroid/issues/68#issuecomment-27187121.

joshuapinter avatar Mar 23 '16 20:03 joshuapinter

Yeah, that's not AA, and I should be able to do that simply by incrementing the version. I'm currently having a nightmare of AA trying to be too "smart".

If you use programatic configuration, you might be able to reset the database before starting up AA.

deleteDatabase("old_db.db");

Configuration dbConfiguration = new Configuration.Builder(this)
                .setDatabaseName("new_db.db")
                .setDatabaseVersion(12)
                .addModelClasses(MyClass.class)
                .create();
ActiveAndroid.initialize(dbConfiguration);

You do that in the Application.onCreate method, and it should emulate a drop.

bpappin avatar Mar 23 '16 20:03 bpappin

@bpappin Incrementing the version allows you to handle migrations nicely.

Your code sample should do the trick, but there shouldn't be a need to have a different database name, right? Once you delete the old one, it'll just re-create a new one with the same name.

joshuapinter avatar Mar 23 '16 20:03 joshuapinter