4

I'm trying to delete a database entirely if onUpgrade gets called. According to this SO question: How to delete SQLite database from Android programmatically I can just call context.deleteDatabase(String dbName), but I can't get to context in the onUpgrade method. In onUpgrade there is a db object that is passed in, and I see that you can use a db.deleteDatabase() method, but it takes a file. I don't have the db file. Is there any way to do db.deleteSelf or something similar? I know that I could just drop tables and such instead of totally deleting the database, but my db structure has totally changed and I would rather just call deleteDb(); and then onCreate(db); again.

Community
  • 1
  • 1
EGHDK
  • 17,818
  • 45
  • 129
  • 204

3 Answers3

4

I'm trying to delete a database entirely if onUpgrade gets called.

Bear in mind that wiping out all the user's data may make the user very cranky, as in "one star ratings on the Play Store"-level cranky.

In addition, what you want is rather risky. You may screw up the schema version metadata. Even assuming that what you are trying to do is sensible in general, I would recommend that you simply call execSQL() on the SQLiteDatabase to execute DROP TABLE statements for every table that you created (plus DROP INDEX, etc.).

I can just call context.deleteDatabase(String dbName), but I can't get to context in the onUpgrade method

Your SQLiteOpenHelper subclass is passed a Context in its constructor. While that is not exposed through a getter method, you could hold the Context yourself in a data member.

I know that I could just drop tables and such instead of totally deleting the database, but my db structure has totally changed and I would rather just call deleteDb(); and then onCreate(db); again.

Except that calling onCreate() will not create your database. You will have deleted the database. So, unless you create an empty database file yourself, onCreate() will crash. onCreate() is called on the event of creating the database; it is not "create me a database".

Also, if this app is still in development, just uninstall the app before running it again, which will wipe out the database (assuming it is stored in a typical location).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • So what if I drop a table in onUpgrade and then call onCreate(db); It shouldn't crash because it still has a valid db? – EGHDK Sep 01 '14 at 19:18
  • 1
    `onUpgrade` is called within an active transaction; you cannot delete the file at this point. (The file would actually be delete when the last file handled is closed, which would be after the `onCreate` call.) – CL. Sep 01 '14 at 19:21
  • @EGHDK: Yes, deleting the *contents* of a database (e.g., `DROP TABLE`) and chaining to `onCreate()` is safe. Just watch out for the users marching on your location with torches and pitchforks. – CommonsWare Sep 01 '14 at 19:35
  • Got it. Thanks for the head up.The users didn't actually have access to do anything with the db. I simply used the db to ship data with the app. – EGHDK Sep 01 '14 at 19:42
1

When you do not care about the old data, you do not actually need the old database for creating the new one. Just give the new database a different name, and it will be created normally. You can delete the old database file(s) whenever you like (i.e., when starting the main activity, when you have a Context), and do not need to care whether this happens before or after you access the new database.

CL.
  • 173,858
  • 17
  • 217
  • 259
0

So I guess your Database-Class extends SQLiteOpenHelper and through this a default constructor of the Type SQLiteOpenHelper(context:Context,name:String,factory:CursorFactory,version:int) is required. This basically means that you need to pass a Context to your Database-Class when creating an instance. This is your option to save the context-reference to local variable:

public class DatabaseHelper extends SQLiteOpenHelper {

    private Context mCtx;

    public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        this.mCtx = context;
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {
        mCtx.deleteDatabase("DB_NAME")
    }
}
reVerse
  • 35,075
  • 22
  • 89
  • 84