Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List Attached Databases using a SELECT command in SQLite

Is there a SELECT command that can list all attached databases similar to the .database command available in sqlite3?

like image 530
galford13x Avatar asked Mar 18 '10 21:03

galford13x


People also ask

How do I list all SQLite databases?

If you are running the sqlite3 command-line access program you can type ". tables" to get a list of all tables. Or you can type ". schema" to see the complete database schema including all tables and indices.

How view SQLite database from command-line?

Open a command prompt (cmd.exe) and 'cd' to the folder location of the SQL_SAFI. sqlite database file. run the command 'sqlite3' This should open the SQLite shell and present a screen similar to that below.


2 Answers

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!

like image 148
Tim C Avatar answered Sep 29 '22 13:09

Tim C


The accepted answer was correct when it was posted, but in SQLite 3.16.0 and later most of the side-effect free pragmas can also be accessed as so called pragma functions.

Which means you can write:

sqlite> .headers on
sqlite> select * from pragma_database_list;
seq|name|file
0|main|
2|a|D:\a.sqlite
3|b|D:\b.sqlite
4|c|D:\c.sqlite

.headers on is completely optional, but very useful, since the column names these pragma functions return are not documented anywhere.

Be aware though, part of the reason they are undocumented is 'This feature is experimental and is subject to change'.

like image 23
szmate1618 Avatar answered Sep 29 '22 13:09

szmate1618