SQLite allows to attach external SQLite databases (i.e. other database files) to a currently connected database. Using such a feature, it is possible to manipulate the data from different databases inside one connection, e.g. writing heterogeneous SQL queries like joins, unions etc. Please note that attaching external databases does not mean physically merging the corresponding files, this is just linking them to access the data.
Database Tour application supports this possibility. But there are some things you need to know in order to use this opportunity correctly and effectively.
Attaching a database
After connecting to a SQLite database, it is possible to attach another database to it. Currently, this must be done by SQL. So, create a new SQL window by clicking New SQL Window button
, choosing Query | New... menu, or pressing Ctrl+Q. Then write and execute the command like this:
ATTACH DATABASE "C:\data sources\sales.db" AS sales;
Here, you specify the path to the database file to attach and the alias for the attached database. You will need that alias to access the tables from the attached database.
Notes
- SQLite requires to attach/detach databases in auto-commit mode. To turn this mode on or off, choose Options | Environment... menu, select Database section, and then use the corresponding switch there. In the current Database Tour version, you have to reconnect the database to apply the changes to auto-commit mode.
- Accessing the attached database is possible only from one database session. In Database Tour, each SQL or table window opens a separate session by default. To enable access to the attached database from all windows of the connected database, turn off the Asynchronous query execution option from the Database section; it is recommended to reconnect the database after changing this option. You will still be able to work with attached database without turning off this option, but that will be possible only from the window, where you executed the ATTACH DATABASE command.
Working with attached databases
After attaching a database and clicking Refresh button above the table list, you will able to see tables from the attached database there; the table names will be prefixed with the attached database alias followed by a period. Clicking such a table will open it in a separate Table window just like it is done for all other tables of the open database. Note: All that will be possible only if you turned off the Asynchronous query execution option (see the notes above).
There will be possible to join the tables from different databases in one SQL command, for example:
SELECT
o.order_date,
c.client_name,
o.order_sum,
o.order_vat,
o.notes
FROM clients c
JOIN [sales].orders o ON o.client_id = c.client_id
ORDER BY 1, 2;
The results of such combined queries can be exported, used in reports etc.
It will be also possible to insert, update, or delete records in tables of the attached database as well as create or delete table etc.
Detaching a database
To detach an attached database, execute the SQL command like this:
DETACH DATABASE sales;
Here, you must specify the alias of the attached database.
Notes
- It is recommended to detach the database in the same SQL window where it was attached.
- After closing the current connection, all attached databases are detached automatically.
See also




