Given an sqlite3 connection object, how can retrieve the file path to the sqlite3 file?
We can use the PRAGMA database_list command.
cur = con.cursor()
cur.execute("PRAGMA database_list")
rows = cur.fetchall()
for row in rows:
print(row[0], row[1], row[2])
The third parameter (row[2]) is the file name of the database. Note that there could be more databases attached to SQLite engine.
$ ./list_dbs.py
0 main /home/user/dbs/test.db
2 movies /home/user/dbs/movies.db
The above is a sample output of a script that contains the Python code.