Android version of Android SQLite with deleted table

In my Android application, I am using SQLite DB with SQLiteOpenHelper

I have multiple DB versions and I was doing operations onUpgrade()

switching the old db version, but now I need to delete one of the tables because I no longer use it, so should I do something else?

public void onCreate(SQLiteDatabase database) {
        database.execSQL(tableUserCreate);
        database.execSQL(tableProductsCreate);
        database.execSQL(tablePicturesCreate);
    }


public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
    switch (version_old) {
    case 1:
        database.execSQL(addPostcodeFieldToUserTable);
        database.execSQL(tablePlacesCreate);
        // Intentional fallthrough, no break;
    case 2:
        database.execSQL(tableProductVideosCreate);
        break;
    }
} // End of onUpgrade

      

Now I want to drop the User table in the new DB version . What should I do?

+3


source to share


2 answers


SQL to drop a table:

DROP TABLE table_name

      



Or use:

DROP TABLE IF EXISTS table_name

      

+3


source


When you change Database_Version it is called

onUpdate (SQLiteDatabase db, int oldVersion, int newVersion)



so you need to do whatever it takes to remove or create stuff inside this method. In your case, you are using the Switch case, I am doing the same using if else. But both work fine in this case.

if (newVersion == yourNewVersion) {

try {

// Ditch the old table if it exists

db.execSQL ("DROP TABLE IF EXISTS" + YOUR_TABLE_NAME);

// Create tables again IF YOU WANT TO CREATE HERE

} catch (Exception e) {e.printStackTrace ();

}

+2


source







All Articles