SQLite classic ...login
SQLite classic tutorial
author:php.cn  update time:2022-04-13 17:05:02

SQLite detached database


SQLite's DETACH DTABASE statement is used to detach and detach the named database from a database connection that was previously attached using the ATTACH statement. If multiple aliases have been attached to the same database file, the DETACH command will only disconnect the given name, while the rest will remain valid. You cannot detach a main or temp database.

If the database is in memory or a temporary database, the database will be destroyed and the 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 before to attach the database using the ATTACH statement.

Example

Assuming that you have created a database in the previous chapter and attached 'test' and 'currentDB' to it, using the .database command, we can see:

sqlite>.databases
seq name                                                                                                                                                -----------
0 main /home/sqlite/testDB.db
2 test /home/sqlite/testDB.db
3 currentDB /home/sqlite/testDB.db
Now, let us 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                                                                                                                                           ------------
0 main /home/sqlite/testDB.db
2 test /home/sqlite/testDB.db


##