How to clear all data in DB?
Is there any method to clear all data??
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>");
Within an activity: this.deleteDatabase("your_db");
@rothschild86 Does this require you to "restart" your app in order for the database to be regenerated?
yeah, i have to restart. don't remember for what reason. if i remember, AA assumes that db still exists and crashes otherwise.
@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!
for sure. i have it hooked up to "Log out", so it's not really jarring to the user that the app closes.
@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.
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 Err.... AA does handle it. See https://github.com/pardom/ActiveAndroid/issues/68#issuecomment-27187121.
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 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.