Sqlite Get Name of Attached Databases

sqlite get name of attached databases

Are you looking for this?

PRAGMA database_list;

PRAGMA database_list;
This pragma works like a query to return one row for each database
attached to the current database connection.
The second column is the
"main" for the main database file, "temp" for the database file used
to store TEMP objects, or the name of the ATTACHed database for other
database files. The third column is the name of the database file
itself, or an empty string if the database is not associated with a
file.

List Attached Databases using a SELECT command in SQLite

You cannot do this with a SELECT statement that I know of (though you might want to look around in the main database, this data might be stored there). However, there is a solution. If you execute the following statement it will return the databases attached for the current connection:

PRAGMA database_list;

The first row will always be the main database, the second will always be the temp database. Any further databases are after these first two. You can run this statement against your database the same way you would a SELECT statement from your code in c# (or anything else for that matter).

Here is a good reference:

SQLite PRAGMA statement reference

Good luck!

How can I list the tables in a SQLite database file that was opened with ATTACH?

The .tables, and .schema "helper" functions don't look into ATTACHed databases: they just query the SQLITE_MASTER table for the "main" database. Consequently, if you used

ATTACH some_file.db AS my_db;

then you need to do

SELECT name FROM my_db.sqlite_master WHERE type='table';

Note that temporary tables don't show up with .tables either: you have to list sqlite_temp_master for that:

SELECT name FROM sqlite_temp_master WHERE type='table';

SQLite / .NET: Get table names for attached database

GetSchema does not support attached databases.
You have read the table names directly from the system table:

SELECT name FROM MyOtherDB.sqlite_master WHERE type = 'table'


Related Topics



Leave a reply



Submit