blog

Home / DeveloperSection / Blogs / SQLite - DETACH DATABASE COMMAND

SQLite - DETACH DATABASE COMMAND

Tarun Kumar3101 15-Sep-2015

The DETACH DATABASE statement  is  used  to  detach  and  dissociate a  named  database from a database  connection  which  was  previously  attached  using  ATTACH  statement.  If  thesame  database  file  hasbeen  attached  with  multiple  aliases,  then  DETACH  command  willdisconnect  only  given  name  and  rest  of  the attachment will still continue. You cannot detach the main or temp databases.


If the database is an in- memory or temporary database, the database will be destroyed and the contents will be lost.

 

Syntax: Basic syntax of SQLite DETACH DATABASE'Alias - Name' statement is as follows:

DETACH DATABASE 'Alias- Name';


Here 'Alias-Name' is the same alias, which you had used while attaching


database using ATTACH statement.

 

Example: Consider you have a database, which you created in previous


chapter and attached it with 'test' and 'currentDB'



as we can see using .database command:


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 will check current attachment, you will find that testDB.db is still connected with 'test' and 'main'.

 sqlite>.databases


seq

name

file

0

main

/home/sqlite/testDB.db

2

test

/home/sqlite/testDB.db

 


Updated 13-Mar-2018

Leave Comment

Comments

Liked By