Sqlite Attach Database
# SQLite Attach Database
Consider a scenario where multiple databases are available at the same time, and you want to use any one of them. The **ATTACH DATABASE** statement in SQLite is used to select a specific database. After using this command, all subsequent SQLite statements will be executed within the attached database.
## Syntax
The basic syntax of SQLite's ATTACH DATABASE statement is as follows:
ATTACH DATABASE file_name AS database_name;
If the database does not already exist, the above command will create a new database. If the database already exists, it binds the database file name with the logical database 'Alias-Name'.
The opened database and the database attached using ATTACH must be located in the same folder.
## Example
If you want to attach an existing database **testDB.db**, the ATTACH DATABASE statement would look like this:
sqlite> ATTACH DATABASE 'testDB.db' as 'TEST';
Use the SQLite **.database** command to display the attached databases.
sqlite> .database seq name file --- --------------- ----------------------0 main /home/sqlite/testDB.db 2 test /home/sqlite/testDB.db
The database names **main** and **temp** are reserved for the main database and the database used to store temporary tables and other temporary data objects. These two database names can be used for every database connection and should not be used for attaching, otherwise you will get a warning message, as shown below:
sqlite> ATTACH DATABASE 'testDB.db' as 'TEMP';Error: database TEMP is already in use sqlite> ATTACH DATABASE 'testDB.db' as 'main';Error: database main is already in useοΌ
YouTip