2

I am working on an Android app, and from time-to-time I need to wipe the SQLite database to start from a clean slate. After some googleing, I found:

context.deleteDatabase(DATABASE_NAME);

However, whenever I execute this statement, then [attempt] re-create my database, my app always starts up empty, with no data. The rest of the UI is there, it's just there is no data. To create the databse, I am using the standard

DatabaseHelper myDBHelper = new DatabaseHelper(this);

which extends SQLiteOpenHelper, that I have seen so many times here on StackOverflow.

As an FYI, when I do not execute the deleteDatabase() statement, all my data loads perfectly, so I know my DatabaseHelper class is working properly.

Also, the reason I am trying to clear the DB is because whenever I build my application in Android Studio, it always adds to the database instead of deleting it, then inserting fresh.

Community
  • 1
  • 1
Brian
  • 1,726
  • 2
  • 24
  • 62
  • As an alternative, **IF** you're only testing on you're own device, I will just delete the app from my device which deletes the database as well and rerun it, and it will then recreate the database. I've never experienced this problem here. How do you know when to call `context.deleteDatabase()`? Is it a line you just comment out when you don't need it – AdamMc331 Dec 18 '15 at 15:54
  • Yes, it is a line I comment out when I don't need it. Also, the reason I try to clear the DB is because whenever I build my application in Android Studio, it always adds to the database instead of deleting it, then inserting fresh. – Brian Dec 18 '15 at 16:05
  • 1
    If the database is open when you request the delete, try closing it before deleting. There are `close()` methods on both `SQLiteOpenHelper` and `SQLiteDatabase` (inherited from `SQLiteClosable`). – Bob Snyder Dec 19 '15 at 06:44
  • I'll look to see if this is the case ... thanks. – Brian Dec 21 '15 at 03:08
  • @qbix Yes, I am closing the DB after every transaction. – Brian Dec 21 '15 at 14:52

0 Answers0