Suppose that when multiple databases are available at the same time, you want to use any of them. SQLite’s ATTACH DATABASE Statement is used to select a specific database, and after using this command, all SQLite statements will be executed under the attached database. The basic syntax of the ATTACH DATABASE statement for SQLite is as follows: If the database has not been created, the above command creates a database, and if the database already exists, bind the database file name to the logical database ‘Alias-Name’. The open database and the database attached using ATTACH must be in the same folder. If you want to attach an existing database testDB.db The ATTACH DATABASE statement will look like this Use SQLite .database Command to display additional databases. Database name main And temp Is reserved for the master database and the database that stores temporary tables and other temporary data objects. These two database names can be used for each database connection and should not be used for attachment, otherwise you will get a warning message, as shown below: 1.8.1. Grammar ¶
ATTACH DATABASE file_name AS database_name;
1.8.2. Example ¶
sqlite> ATTACH DATABASE 'testDB.db' as 'TEST';
sqlite> .database
seq name file
--- --------------- ----------------------
0 main /home/sqlite/testDB.db
2 test /home/sqlite/testDB.db
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;