Question

I'm trying to drop my tables onUpgrade, and it does delete the empty tables, but if tables have rows in it - it doesn't get deleted.

I've tried the following code to clean the table first:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.delete(EventsTable.TABLE_NAME,null,null);
    db.delete(UsersTable.TABLE_NAME,null,null);
    //Create the drop query
    String dropQuery = "Drop Table " + EventsTable.TABLE_NAME + ";";
    dropQuery += " Drop Table " + UsersTable.TABLE_NAME + ";";                      
    // Drop older tables if existed     
    db.execSQL(dropQuery);
    // Create tables again
    onCreate(db);       
}   

But still, the table doesn't drop - and it will cause me problem with releasing a new version of my application since my DB has changed

Was it helpful?

Solution

String dropQuery = "Drop Table " + EventsTable.TABLE_NAME + ";";
dropQuery += " Drop Table " + UsersTable.TABLE_NAME + ";";  

You are trying to execute multiple SQL statement which is not allowed in sqlite. I suggest you to execute both drop table sql statement separately.

String dropQuery = "Drop Table " + EventsTable.TABLE_NAME + ";";
db.execSQL(dropQuery);
dropQuery = " Drop Table " + UsersTable.TABLE_NAME + ";";
db.execSQL(dropQuery);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top