SQLite’s DETACH DATABASE Statement is used to separate and detach a named database from a database connection, which was previously attached using an ATTACH statement. If more than one alias has been attached to the same database file, the DETACH command will only disconnect the given name, while the rest are still valid. You can’t separate. main Or temp Database.
If the database is in memory or a tempdb, the database will be destroyed and the contents will be lost. The basic syntax of the DETACH DATABASE ‘Alias-Name’ statement for SQLite is as follows: Here, ‘Alias-Name’ is the same alias you used to attach the database using the ATTACH statement. Assuming that you have created a database in the previous section and attached the ‘test’ and’ currentDB’, using .database commands, we can see: Now, let’s try to separate ‘currentDB’ from the testDB.db, as follows: Now, if you check the currently attached database, you will find that testDB.db is still connected to ‘test’ and’ main’. 1.9.1. Grammar ¶
DETACH DATABASE 'Alias-Name';
1.9.2. Example ¶
sqlite>.databases
seq name file
--- --------------- ----------------------
0 main /home/sqlite/testDB.db
2 test /home/sqlite/testDB.db
3 currentDB /home/sqlite/testDB.db
sqlite> DETACH DATABASE 'currentDB';
sqlite>.databases
seq name file
--- --------------- ----------------------
0 main /home/sqlite/testDB.db
2 test /home/sqlite/testDB.db