English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

SQLite separates databases (DETACH DATABASE)

The SQLite DETACH DATABASE statement is used to separate and dissociate the named database from the database connection attached with the ATTACH statement previously used. If the same database file is attached with multiple aliases, the DETACH command will only disconnect the connection given by the name, and the rest of the attachments will continue. You cannot detachmainortempDatabase.

If the database is a memory database or a temporary database, the database will be destroyed and the content will be lost.

Syntax

The following is the SQLite DETACH DATABASE 'Alias-Name' statement syntax.

DETACH DATABASE 'Alias-Name';

Here, "Alias-"Name" is the alias you use when attaching a database with the ATTACH statement.

Online Examples

Assuming you have a database, you created the database in the previous chapter and attached 'test' and 'currentDB' to the database, we can use.database commandsee it.

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

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

sqlite> DETACH DATABASE 'currentDB';

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

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