anonymous
anonymous

Reputation: 11

How to get a SQLite database name in python

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')]

Upvotes: 0

Views: 5112

Answers (1)

Nick
Nick

Reputation: 7441

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()

Upvotes: 3

Related Questions