Delete all tables from sqlite database

2020-02-10 12:48发布

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

4条回答
家丑人穷心不美
2楼-- · 2020-02-10 13:21

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);
}
查看更多
劳资没心,怎么记你
3楼-- · 2020-02-10 13:28

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());
    }
查看更多
小情绪 Triste *
4楼-- · 2020-02-10 13:34

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();
    }
}
查看更多
走好不送
5楼-- · 2020-02-10 13:47

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);
查看更多
登录 后发表回答