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

SQLite add-on database


Suppose there is a situation when multiple databases are available at the same time and you want to use any one of them. SQLite's ATTACH DTABASE statement is used to select a specific database. After using this command, all SQLite statements will be executed under the attached database.

Syntax

The basic syntax of SQLite's ATTACH DATABASE statement is as follows:

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

If the database has not been created yet, the above command will create a database. If the database already exists, bind the database file name to the logical database 'Alias-Name'.

Example

If you want to attach an existing database testDB.db, the ATTACH DATABASE statement will look like this:

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

Use the SQLite .database command to display the attached database.

sqlite> .database
seq name                                                                                                                                             ------------
0 main /home/sqlite/testDB.db
2 test /home/sqlite/testDB.db
Database name

main and temp are reserved for the main database and the database that stores temporary tables and other temporary data objects. These two database names are available for every database connection and should not be used for appends, otherwise you will get a warning message like this:

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