Python Sqlite3 Get Sqlite Connection path

后端 未结 3 788
囚心锁ツ
囚心锁ツ 2021-02-18 17:59

Given an sqlite3 connection object, how can retrieve the file path to the sqlite3 file?

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-18 18:30

    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.

提交回复
热议问题