SQLite Detach Database

SQLite'sDETACH DATABASEThe statement is used to detach and free 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 only disconnect the connection for the given name, while the rest remain valid. You cannot detachmainortempthe database.

If the database is an in-memory or temporary database, the database 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 earlier.

Example

Suppose you have created a database in the previous chapters 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 follows:

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
Other Extensions