删除所有表命令

发布于 2024-07-13 04:24:27 字数 51 浏览 6 评论 0原文

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

同样,我想删除所有索引。

What is the command to drop all tables in SQLite?

Similarly I'd like to drop all indexes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

打小就很酷 2024-07-20 04:24:27

虽然确实没有 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;

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;
浅浅淡淡 2024-07-20 04:24:27

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

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

此输出是一个将为您删除表的脚本。 对于索引,只需将 'table' 替换为 'index' 即可。

您可以在 where 部分中使用其他子句来限制选择哪些表或索引(例如以 pax_< 开头的 和 name glob 'pax_*' /代码>)。

您可以将此脚本的创建与运行结合到一个简单的 bash(或 cmd)脚本中,这样只需运行一个命令。

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

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) script so there's only one command to run.

If you don't care about any of the information in the database, I think you can just delete the file it's stored in - that's probably faster.

一场春暖 2024-07-20 04:24:27
rm db/development.sqlite3
rm db/development.sqlite3
许久 2024-07-20 04:24:27

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

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);
}
小清晰的声音 2024-07-20 04:24:27

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

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]))
楠木可依 2024-07-20 04:24:27

我想添加涉及删除表而不删除文件的其他答案,您还可以执行 delete from sqlite_sequence 来重置自动增量序列。

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.

潇烟暮雨 2024-07-20 04:24:27

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

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

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.

仙女 2024-07-20 04:24:27

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

因为我在表中使用了 AUTOINCRMENT 主键,所以有一个名为 sqlite_sequence 的表。 当例程尝试删除该表时,SQLite 会崩溃。 我也无法捕捉到异常。 查看 https://www.sqlite.org/fileformat.html#internal_schema_objects,我了解到可能有几个我不想删除的内部架构表。 文档说这些表中的任何一个都有以 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();
    }
}

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();
    }
}
梦幻的味道 2024-07-20 04:24:27

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

.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

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

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.

橘亓 2024-07-20 04:24:27

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

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

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"
那些过往 2024-07-20 04:24:27

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

delete from sqlite_master where type in ('view', 'table', 'index', 'trigger');

To delete also views add 'view' keyword:

delete from sqlite_master where type in ('view', 'table', 'index', 'trigger');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文