1.8. SQLite attached database

发布时间 :2025-10-25 12:31:10 UTC      

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.

1.8.1. Grammar

The basic syntax of the ATTACH DATABASE statement for SQLite is as follows:

ATTACH DATABASE file_name AS database_name;

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.

1.8.2. 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 SQLite .database Command to display additional databases.

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

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:

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;

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.