English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

SQLite Attach Database (ATTACH DATABASE)

Consider a situation where you have multiple available databases and you want to use any one of them at a time. SQLite ATTACH DATABASEThe statement is used to select a specific database, and after executing this command, all SQLite statements will be executed under the attached database.

Syntax

The following is the basic syntax of the SQLite ATTACH DATABASE statement.

ATTACH DATABASE 'DatabaseName' As 'Alias-Name';

If the database has not been created yet, the above command will also create a database; otherwise, it will only attach the database filename to the logical database 'alias'.

Online examples

If you want to attach an existing databasetestDB.dbIf so, the ATTACH DATABASE statement is as follows-

sqlite> ATTACH DATABASE 'testDB.db' as 'TEST';

Using SQLite.databaseThe command displays 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 for retaining temporary tables and other temporary data objects. These database names exist for each database connection and should not be used for attachments; otherwise, you will receive the following warning message.

sqlite> ATTACH DATABASE 'testDB.db' as 'TEMP';
Error: database TEMP is already in use
sqlite> ATTACH DATABASE 'testDB.db' as 'main';
Error: database TEMP is already in use