删除所有表命令

问题描述 投票:0回答:11

删除 SQLite 中所有表的命令是什么?

同样我想删除所有索引。

sql sqlite rdbms database
11个回答
100
投票

虽然确实没有 DROP ALL TABLES 命令,但您可以使用以下命令集。

注意:这些命令有可能损坏您的数据库,因此请确保您有备份

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

然后您想要恢复已删除的空间

VACUUM;

以及一个良好的测试以确保一切正常

PRAGMA INTEGRITY_CHECK;

94
投票

我认为您不能一次删除所有表,但您可以执行以下操作来获取命令:

select 'drop table ' || name || ';' from sqlite_master where type = 'table';

此命令的输出是一个脚本,它将为您删除表。对于索引,只需将

'table'
替换为
'index'

您可以使用

where
部分中的其他子句来限制选择哪些表或索引(例如
and name glob 'pax_*'
表示以
pax_
开头的子句)。

您可以将此脚本的创建与运行结合到一个简单的

bash
(或
cmd
)脚本中,这样只需运行一个命令。

如果您不关心数据库中的任何信息,我认为您可以删除存储它的文件 - 这可能会更快。


37
投票
rm db/development.sqlite3

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

6
投票

使用 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]))

4
投票

我想添加涉及删除表而不删除文件的其他答案,您也可以执行

delete from sqlite_sequence
来重置自动增量序列。


3
投票

一旦你删除了所有表(当表消失时索引就会消失),据我所知,SQLite 数据库中就没有任何东西了,尽管文件似乎没有缩小(根据我刚刚的快速测试)做过)。

因此删除文件似乎是最快的 - 当您的应用程序尝试访问数据库文件时应该重新创建它。


2
投票

我在Android中遇到了这个问题,我写了一个类似于it-west的方法。

因为我在表中使用了

AUTOINCREMENT
主键,所以有一个名为
sqlite_sequence
的表。当例程尝试删除该表时,SQLite 会崩溃。我也无法捕捉到异常。查看 https://www.sqlite.org/fileformat.html#internal_schema_objects,我了解到可能有几个我不想删除的 internal schema 表。文档说这些表中的任何一个都有以 sqlite_ 开头的名称,所以我写了这个方法

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

1
投票

我不能说这是最防弹或最便携的解决方案,但它适用于我的测试脚本:

.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

这段代码将输出重定向到临时文件,构造我想要运行的“删除表”命令(将命令发送到临时文件),将输出设置回标准输出,然后执行文件中的命令,然后最后删除该文件。


0
投票

或者在 shell 提示符下,只需两行,没有命名临时文件,假设 $db 是 SQLite 数据库名称:

echo "SELECT 'DROP TABLE ' || name ||';' FROM sqlite_master WHERE type = 'table';" |
    sqlite3 -readonly "$db" | sqlite3 "$db"

0
投票

要删除视图,请添加“view”关键字:

delete from sqlite_master where type in ('view', 'table', 'index', 'trigger');
© www.soinside.com 2019 - 2024. All rights reserved.