pax*_*blo 77
我不认为您可以在一次点击中删除所有表,但您可以执行以下操作来获取命令:
select 'drop table ' || name || ';' from sqlite_master
where type = 'table';
Run Code Online (Sandbox Code Playgroud)
这个输出是一个将为您删除表的脚本.对于索引,只需将表替换为索引即可.
您可以使用该where
部分中的其他子句来限制选择哪些表或索引(例如,and name glob 'pax_*'
对于以"pax_"开头的那些表格或索引).
您可以将此脚本的创建与在简单的bash(或cmd.exe)脚本中运行它一起组合,因此只能运行一个命令.
如果你不关心数据库中的任何信息,我认为你可以删除它存储在硬盘上的文件 - 这可能更快.我从未测试过这个,但我不明白为什么它不会起作用.
Noa*_*oah 75
虽然没有DROP ALL TABLES命令,但您可以使用以下命令集.
注意:这些命令可能会损坏您的数据库,因此请确保您有备份
PRAGMA writable_schema = 1;
delete from sqlite_master where type in ('table', 'index', 'trigger');
PRAGMA writable_schema = 0;
Run Code Online (Sandbox Code Playgroud)
然后,您想要恢复已删除的空间
VACUUM;
Run Code Online (Sandbox Code Playgroud)
并且一个很好的测试,以确保一切正常
PRAGMA INTEGRITY_CHECK;
Run Code Online (Sandbox Code Playgroud)
ala*_*dey 34
rm db/development.sqlite3
Run Code Online (Sandbox Code Playgroud)
it-*_*net 22
我在SQLite和Android上遇到了同样的问题.这是我的解决方案:
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);
}
Run Code Online (Sandbox Code Playgroud)
使用 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]))
Run Code Online (Sandbox Code Playgroud)