How to delete SQLite database from Android programmatically

前端 未结 12 1590
猫巷女王i
猫巷女王i 2020-11-27 10:41

I would like to delete the database file from the Android file system programatically? Can I have a shell script launch adb which in turns runs a

相关标签:
12条回答
  • 2020-11-27 11:31

    The SQLiteDatabase.deleteDatabase(File file) static method was added in API 16. If you want to write apps that support older devices, how do you do this?

    I tried: file.delete();

    but it messes up SQLiteOpenHelper.

    Thanks.

    NEVER MIND! I later realized you are using Context.deleteDatabase(). The Context one works great and deletes the journal too. Works for me.

    Also, I found I needed to call SQLiteOpenHelp.close() before doing the delete, so that I could then use LoaderManager to recreate it.

    0 讨论(0)
  • 2020-11-27 11:33
    context.deleteDatabase("database_name.db");
    

    This might help someone. You have to mention the extension otherwise, it will not work.

    0 讨论(0)
  • 2020-11-27 11:34

    From Application Manager, you can delete whole application with data. Or just data by it self. This includes database.

    1. Navigate to Settings. You can get to the settings menu either in your apps menu or, on most phones, by pulling down the notification drawer and tapping a button there.

    2. Select the Apps submenu. On some phones this menu will have a slightly different name such as Application Manager.

    3. Swipe right to the All apps list. Ignore the lists of Running and Downloaded apps. You want the All apps list.

    4. Select the app you wish to disable. A properties screen appears with a button for Force Stop on the upper left and another for either Disable or Uninstall updates on the upper right side.

    5. Delete data.

    0 讨论(0)
  • 2020-11-27 11:35

    context.deleteDatabase(DATABASE_NAME); will delete the database only if all the connections are closed. If you are maintaining singleton instance for handling your database helper - it is easy to close the opened Connection.

    Incase the databasehelper is used in multiple place by instantiating directly, the deleteDatabase + killProcess will do the job even if some connections are open. This can be used if the application scenario doesn't have any issues in restarting the app.

    0 讨论(0)
  • 2020-11-27 11:37

    It's easy just type from your shell:

    adb shell
    cd /data/data
    cd <your.application.java.package>
    cd databases
    su rm <your db name>.db
    
    0 讨论(0)
  • 2020-11-27 11:37

    you can create a file object of current database path and then delete it as we delete file from folder

        File data = Environment.getDataDirectory();
        String currentDBPath = "/data/com.example.demo/databases/" + DATABASE_NAME;
        File currentDB = new File(data, currentDBPath);
        boolean deleted = SQLiteDatabase.deleteDatabase(currentDB);
    
    0 讨论(0)
提交回复
热议问题