Sqlite 简明教程

SQLite - DETACH Database

SQLite DETACH DATABASE 语句用于分离和解除与先前使用 ATTACH 语句附加的数据库连接的已命名数据库。如果已使用多个别名附加了相同的数据库文件,DETACH 命令将仅断开提供的名称,其余附加仍然将继续。无法分离 maintemp 数据库。

SQLite DETACH DATABASE statement is used to detach and dissociate a named database from a database connection which was previously attached using ATTACH statement. If the same database file has been attached with multiple aliases, then DETACH command will disconnect only the given name and rest of the attachment will still continue. You cannot detach the main or temp databases.

如果数据库是内存中或临时数据库,数据库将被销毁,并且内容将丢失。

If the database is an in-memory or temporary database, the database will be destroyed and the contents will be lost.

Syntax

以下是 SQLite DETACH DATABASE 'Alias-Name' 语句的基本语法。

Following is the basic syntax of SQLite DETACH DATABASE 'Alias-Name' statement.

DETACH DATABASE 'Alias-Name';

此处,“别名名称”是使用 ATTACH 语句附加数据库时使用的相同别名。

Here, 'Alias-Name' is the same alias, which you had used while attaching the database using ATTACH statement.

Example

考虑你有在上一章中创建的数据库,并使用“测试”和“currentDB”附加它,如我们使用 .database 命令所见。

Consider you have a database, which you created in the previous chapter and attached it with 'test' and 'currentDB' as we can see using .database command.

sqlite>.databases
seq  name             file
---  ---------------  ----------------------
0    main             /home/sqlite/testDB.db
2    test             /home/sqlite/testDB.db
3    currentDB        /home/sqlite/testDB.db

让我们尝试使用以下命令从 testDB.db 中分离 “currentDB”。

Let’s try to detach 'currentDB' from testDB.db using the following command.

sqlite> DETACH DATABASE 'currentDB';

现在,如果你检查当前附件,你会发现 testDB.db 仍然与 “test” 和 “main” 相连。

Now, if you will check the current attachment, you will find that testDB.db is still connected with 'test' and 'main'.

sqlite>.databases
seq  name             file
---  ---------------  ----------------------
0    main             /home/sqlite/testDB.db
2    test             /home/sqlite/testDB.db