SQLite Detach Database
SQLite's DETACH DATABASE The statement is used to detach a named database from a database connection that was previously attached using the ATTACH statement. If the same database file has been attached with multiple aliases, the DETACH command will disconnect only the connection with the given name, while the others remain valid. You cannot detach main or temp databases.
If the database is in memory or is a temporary database, it will be destroyed and its contents will be lost.
Syntax
The basic syntax of SQLite's DETACH DATABASE 'Alias-Name' statement is as follows:
DETACH DATABASE 'Alias-Name';
Here, 'Alias-Name' is the same alias you used when attaching the database with the ATTACH statement.
Example
Suppose you created a database in a previous chapter and attached 'test' and 'currentDB' to it; using the .database command, we can see:
sqlite>.databases
seq name file
--- --------------- ----------------------
0 main /home/sqlite/testDB.db
2 test /home/sqlite/testDB.db
3 currentDB /home/sqlite/testDB.db
Now, let's try to detach 'currentDB' from testDB.db, as shown below:
sqlite> DETACH DATABASE 'currentDB';
Now, if you check the currently attached databases, 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