Camandros
Camandros

Reputation: 459

Drop all tables with exceptions in sqlite

I saw this question where it is presented a command to drop all tables from an sqlite database. Is it possible to add exceptions, i.e., drop all tables except table X Y and Z?

Thanks

EDIT I also saw that it is possible in SQL. Is this script translatable to sqlite?

Upvotes: 2

Views: 2323

Answers (1)

Matthias Wiehl
Matthias Wiehl

Reputation: 1998

PRAGMA writable_schema = 1;
DELETE FROM sqlite_master WHERE type = 'table' AND name NOT IN ('X', 'Y', 'Z');
PRAGMA writable_schema = 0;
VACUUM;

Or, to get the DDL:

SELECT 'DROP TABLE ' || name || ';' FROM sqlite_master
    WHERE type = 'table' AND name NOT IN ('X', 'Y', 'Z');

Upvotes: 6

Related Questions