How to delete SQLite database from Android programmatically

前端 未结 12 1589
猫巷女王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:18

    Try:

    this.deleteDatabase(path); 
    

    or

    context.deleteDatabase(path);
    
    0 讨论(0)
  • 2020-11-27 11:18

    Delete old Db when uninstall the app.

    Setting android:allowBackup="false" in the application tag in AndroidManifest.xml fixed the problem. It seems that for some weird reason the Android OS was restoring from a backup every time I deployed the app.

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

    Once you have your Context and know the name of the database, use:

    context.deleteDatabase(DATABASE_NAME);
    

    When this line gets run, the database should be deleted.

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

    I used Android database delete method and database removed successfully

    public bool DeleteDatabase()
            {
                var dbName = "TenderDb.db";
                var documentDirectoryPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
                var path = Path.Combine(documentDirectoryPath, dbName);
                return Android.Database.Sqlite.SQLiteDatabase.DeleteDatabase(new Java.IO.File(path));
            }
    
    0 讨论(0)
  • 2020-11-27 11:25

    Also from Eclipse you can use DDMS which makes it really easy.

    Just make sure your emulator is running, and then switch to DDMS perspective in Eclipse. You'll have full access to the File Explorer which will allow you to go in and easily delete the entire database.

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

    I have used the following for "formatting" the database on device after I have changed the structure of the database in assets. I simply uncomment the line in MainActivity when I wanted that the database is read from the assets again. This will reset the device database values and structure to mach with the preoccupied database in assets folder.

        //database initialization. Uncomment to clear the database
        //deleteDatabase("questions.db");
    

    Next, I will implement a button that will run the deleteDatabase so that the user can reset its progress in the game.

    0 讨论(0)
提交回复
热议问题