0

When my app starts, it copys sqlite database from assets folder to internal storage. and it works fine, but I need to upgrade my database, in that case, I have to delete my database in user's phone. How can I delete it programmatically? Thank you in advance.

John
  • 83
  • 1
  • 2
  • 9
  • With the [link](https://stackoverflow.com/a/3406632/9629748) about runtime permission. You can not delete asset resource directly, but you can `upgrade your app with new sqlite database to overwrite the older in asset`. And you may need to write some codes for migrations of tables change, columns change and others. – Hababa Jun 13 '20 at 17:22
  • Thank you for your reply! When I consider your opinion on writing some codes for migration of table change, I am entry level programmer, so I can not think other way not to rerun the migration codes whenever user runs my app, so I thought it is best to just delete the db inside user's internal storage not from assets. Do you know how to delete db inside user's internal storage? – John Jun 13 '20 at 17:42
  • You can try `context.deleteDatabase(DATABASE_NAME);`, or if you know the database path, you can delete the .db file directly too. – Hababa Jun 14 '20 at 14:17
  • Thank you for your reply!. I tried with your answer. but it didn't work. My code is like this. getApplicationContext().deleteDatabase(ROOT_DIR + "/" + dbName); I think it just erased the content of the file so the size of the file is 0. but the file still exists. so when I run this code checkDB = new File(ROOT_DIR + "/" + dbName); if (!checkDB.exists()) { here it says I have a file even if the size of the file is 0. Do you know any other way to resolve this issue? – John Jun 15 '20 at 06:50
  • I tried this code too. and it didn't work... File f = new File(ROOT_DIR + "/" + dbName); if(f.delete()){ Log.d("deleteFile", "test"); } getApplicationContext().deleteDatabase(ROOT_DIR + "/" + dbName); Do you know any other way to resolve this? – John Jun 15 '20 at 07:01
  • Where is your `ROOT_DIR`, is it not in your app directory such as `data/data/{app_package}/your_database.db`? You can check it with `DeviceFileExplorer` to find the real-path. – Hababa Jun 15 '20 at 08:32
  • I googled it and find out i need to used deletefile(dbname) not delete() now it works. Thank you for your comment. – John Jun 15 '20 at 09:14

0 Answers0