Drop all tables command

后端 未结 10 545
滥情空心
滥情空心 2020-11-28 23:52

What is the command to drop all tables in SQLite?

Similarly I\'d like to drop all indexes.

相关标签:
10条回答
  • 2020-11-28 23:55

    Once you've dropped all the tables (and the indexes will disappear when the table goes) then there's nothing left in a SQLite database as far as I know, although the file doesn't seem to shrink (from a quick test I just did).

    So deleting the file would seem to be fastest - it should just be recreated when your app tries to access the db file.

    0 讨论(0)
  • 2020-11-28 23:55

    I had this issue in Android and I wrote a method similar to it-west.

    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 didn't want to 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();
        }
    }
    
    0 讨论(0)
  • 2020-11-28 23:55

    I can't say this is the most bulletproof or portable solution, but it works for my testing scripts:

    .output /tmp/temp_drop_tables.sql
    select 'drop table ' || name || ';' from sqlite_master where type = 'table';
    .output stdout
    .read /tmp/temp_drop_tables.sql
    .system rm /tmp/temp_drop_tables.sql
    

    This bit of code redirects output to a temporary file, constructs the 'drop table' commands that I want to run (sending the commands to the temp file), sets output back to standard out, then executes the commands from the file, and finally removes the file.

    0 讨论(0)
  • 2020-11-28 23:58

    While it is true that there is no DROP ALL TABLES command you can use the following set of commands.

    Note: These commands have the potential to corrupt your database, so make sure you have a backup

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

    you then want to recover the deleted space with

    VACUUM;
    

    and a good test to make sure everything is ok

    PRAGMA INTEGRITY_CHECK;
    
    0 讨论(0)
  • 2020-11-29 00:02

    I'd like to add to other answers involving dropping tables and not deleting the file, that you can also execute delete from sqlite_sequence to reset auto-increment sequences.

    0 讨论(0)
  • 2020-11-29 00:02

    Using pysqlite:

    tables = list(cur.execute("select name from sqlite_master where type is 'table'"))
    
    cur.executescript(';'.join(["drop table if exists %s" %i for i in tables]))
    
    0 讨论(0)
提交回复
热议问题