0

Is there a way I can get a SQLite database name in Python.

I am using the following code to get the database name:

con=sqlite3.connect(":memory:")
cursor = con.cursor()
cursor.execute("PRAGMA database_list;")
curr_table = cursor.fetchall()

But the output I get is:

[(0, u'main', u'')]

When I should be getting something like

[(0,u'main',u'/Users/testdb.db'),                           
(2,u'test',u'/Users/testdb.db')]
7
  • what? ... I dont understand the question ... sqlite has a filename(or in :memory) as the database name ... do you mean get the table names? Commented Jun 30, 2016 at 23:05
  • I used this code con=sqlite3.connect(":memory:") cursor = con.cursor() cursor.execute("PRAGMA database_list;") But it's not listing the correct databases Commented Jun 30, 2016 at 23:07
  • what do you mean? what does it list and what do you expect? ... this is not a reproduceable example as it stands ... Commented Jun 30, 2016 at 23:10
  • I've modified the question. Hope it is better Commented Jun 30, 2016 at 23:17
  • 1
    Those databases are not associated with your connection that is using :memory: so you won't see them. Read the docs: sqlite.org/pragma.html#pragma_database_list Commented Jun 30, 2016 at 23:19

1 Answer 1

3

You should be connecting to the actual file - otherwise you cannot view the databases within it.

So, you just need to change your code to this:

con=sqlite3.connect('/Users/testdb.db')
cursor = con.cursor()
cursor.execute("PRAGMA database_list;")
curr_table = cursor.fetchall()
Sign up to request clarification or add additional context in comments.

3 Comments

But this won't give all the databases it's just giving me the output as:[(0, u'main', u'/Users/testdb.db')]
@anonymous - You can only see the databases within the file that you are connected to. SQLite is a file-based database, so you aren't connecting to a server at all. Is your file testdb.db an actual SQLite database file?
If you only see one database, that is because there is only one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.