0

I am using a ContentProvider. My SQLiteOpenHelper class contains the method

public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + table1);
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + table2);
        onCreate(sqLiteDatabase);
    }

Say I am in a situation where I do not remember the name of the tables (I lost them somehow. Play with me). How do I delete all of them from inside the method onUpgrade? I see mention of

PRAGMA writable_schema = 1;
delete from sqlite_master where type in ('table', 'index', 'trigger');
PRAGMA writable_schema = 0;

But I don't know how to apply it to the onUpgrade method.

Katedral Pillon
  • 14,534
  • 25
  • 99
  • 199

1 Answers1

3

You can write following code in upgrade method.

Not sure about how to use drop table query in onUpgrade.

Try this hope you may get any idea by using following code.

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String alterTableQuery = "ALTER TABLE " + TABLE_NAME + " ADD COLUMN " + COULMN_NAME + " INT DEFAULT 0 ";
        db.execSQL(alterTableQuery);
    }

There is also one way to replace database by using drop all table and call onCreate method after this.

You can use following code for it.

public void deleteall(){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor c = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
    do
    {
        db.delete(c.getString(0),null,null);
    }while (c.moveToNext());
}

Delete all tables from sqlite database

Community
  • 1
  • 1
Kailas Bhakade
  • 1,902
  • 22
  • 27