Sqlite 简明教程
SQLite - ATTACH Database
考虑一个案例:您有多个可用的数据库,您只想每次使用其中一个。SQLite ATTACH DATABASE 语句用于选择特定数据库,此命令之后,所有的 SQLite 语句都将在所附加的数据库中执行。
Consider a case when you have multiple databases available and you want to use any one of them at a time. SQLite ATTACH DATABASE statement is used to select a particular database, and after this command, all SQLite statements will be executed under the attached database.
Syntax
以下是 SQLite ATTACH DATABASE 语句的基本语法。
Following is the basic syntax of SQLite ATTACH DATABASE statement.
ATTACH DATABASE 'DatabaseName' As 'Alias-Name';
如果数据库尚未创建,以上命令还会创建一个数据库,否则只将数据库文件名附加到逻辑数据库“别名”。
The above command will also create a database in case the database is already not created, otherwise it will just attach database file name with logical database 'Alias-Name'.
Example
如果您希望附加一个现有数据库 testDB.db ,则 ATTACH DATABASE 语句如下 −
If you want to attach an existing database testDB.db, then ATTACH DATABASE statement would be as follows −
sqlite> ATTACH DATABASE 'testDB.db' as 'TEST';
使用 SQLite .database 命令显示附加的数据库。
Use SQLite .database command to display attached database.
sqlite> .database
seq name file
--- --------------- ----------------------
0 main /home/sqlite/testDB.db
2 test /home/sqlite/testDB.db
数据库名 main 和 temp 分别为主要数据库和保存临时表和其他临时数据对象的数据库保留。这两个数据库名每个数据库连接都存在,不应用于附加,否则您将收到以下警告信息。
The database names main and temp are reserved for the primary database and database to hold temporary tables and other temporary data objects. Both of these database names exist for every database connection and should not be used for attachment, otherwise you will get 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