What is the command to drop all tables in SQLite?
Similarly I\'d like to drop all indexes.
Or at a shell prompt, in just two lines, without a named temporary file, assuming $db is the SQLite database name:
echo "SELECT 'DROP TABLE ' || name ||';' FROM sqlite_master WHERE type = 'table';" |
sqlite3 -readonly "$db" | sqlite3 "$db"
I had the same problem with SQLite and Android. Here is my Solution:
List<String> tables = new ArrayList<String>();
Cursor cursor = db.rawQuery("SELECT * FROM sqlite_master WHERE type='table';", null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
String tableName = cursor.getString(1);
if (!tableName.equals("android_metadata") &&
!tableName.equals("sqlite_sequence"))
tables.add(tableName);
cursor.moveToNext();
}
cursor.close();
for(String tableName:tables) {
db.execSQL("DROP TABLE IF EXISTS " + tableName);
}
rm db/development.sqlite3
I don't think you can drop all tables in one hit but you can do the following to get the commands:
select 'drop table ' || name || ';' from sqlite_master
where type = 'table';
The output of this is a script that will drop the tables for you. For indexes, just replace table with index.
You can use other clauses in the where
section to limit which tables or indexes are selected (such as "and name glob 'pax_*'
" for those starting with "pax_").
You could combine the creation of this script with the running of it in a simple bash (or cmd.exe) script so there's only one command to run.
If you don't care about any of the information in the DB, I think you can just delete the file it's stored in off the hard disk - that's probably faster. I've never tested this but I can't see why it wouldn't work.