19

I have done a lot of research and was unable to find a suitable method to delete all the tables in an SQLite database. Finally, I did a code to get all table names from the database and I tried to delete the tables using the retrieved table names one by one. It didn't work as well.

Please suggest me a method to delete all tables from the database.

This is the code that I used:

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());
}

function deleteall() is called on button click whos code is given as below:

public void ButtonClick(View view)
{
    String Button_text;
    Button_text = ((Button) view).getText().toString();

    if(Button_text.equals("Delete Database"))
    {
        DatabaseHelper a = new DatabaseHelper(this);
        a.deleteall();
        Toast.makeText(getApplicationContext(), "Database Deleted Succesfully!", Toast.LENGTH_SHORT).show();
    }}
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
Tony Mathew
  • 880
  • 1
  • 12
  • 35
  • 1
    Can't you just delete the whole database file? http://stackoverflow.com/questions/4406067/how-to-delete-sqlite-database-from-android-programmatically?rq=1 – Thilo Jul 30 '16 at 09:59
  • @TimBiegeleisen: I want to remove all tables from the database, not just delete the data in the tables.Please suggest me a method to remove all the tables. – Tony Mathew Jul 30 '16 at 10:16
  • Check with accepted answer in this link.http://stackoverflow.com/questions/25641350/how-to-delete-all-tables-from-sqlite-in-android – Pravin Divraniya Jul 30 '16 at 11:12

4 Answers4

26

Use DROP TABLE:

// query to obtain the names of all tables in your database
Cursor c = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
List<String> tables = new ArrayList<>();

// iterate over the result set, adding every table name to a list
while (c.moveToNext()) {
    tables.add(c.getString(0));
}

// call DROP TABLE on every table name
for (String table : tables) {
    String dropQuery = "DROP TABLE IF EXISTS " + table;
    db.execSQL(dropQuery);
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
7

Tim Biegeleisen's answer almost worked for me, but because I used AUTOINCREMENT primary keys in my tables, there was a table called sqlite_sequence. SQLite would crash when the routine tried to drop that table. I couldn't catch the exception either. Looking at https://www.sqlite.org/fileformat.html#internal_schema_objects, I learned that there could be several of these internal schema tables that I shouldn't drop. The documentation says that any of these tables have names beginning with sqlite_ so I wrote this method

private void dropAllUserTables(SQLiteDatabase db) {
    Cursor cursor = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
    //noinspection TryFinallyCanBeTryWithResources not available with API < 19
    try {
        List<String> tables = new ArrayList<>(cursor.getCount());

        while (cursor.moveToNext()) {
            tables.add(cursor.getString(0));
        }

        for (String table : tables) {
            if (table.startsWith("sqlite_")) {
                continue;
            }
            db.execSQL("DROP TABLE IF EXISTS " + table);
            Log.v(LOG_TAG, "Dropped table " + table);
        }
    } finally {
        cursor.close();
    }
}
Jon
  • 9,156
  • 9
  • 56
  • 73
2

delete database instead of deleting tables and then create new with same name if you need. use following code

context.deleteDatabase(DATABASE_NAME); 
          or
context.deleteDatabase(path);
Ganesh Karewad
  • 1,158
  • 12
  • 21
2

For me, the working solution is:

    Cursor c = db.rawQuery(
            "SELECT name FROM sqlite_master WHERE type IS 'table'" +
                    " AND name NOT IN ('sqlite_master', 'sqlite_sequence')",
            null
    );
    if(c.moveToFirst()){
        do{
            db.execSQL("DROP TABLE " + c.getString(c.getColumnIndex("name")));
        }while(c.moveToNext());
    }
atlascoder
  • 2,746
  • 3
  • 26
  • 34