Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Sqlite3获取Sqlite连接路径_Python_Sqlite - Fatal编程技术网

Python Sqlite3获取Sqlite连接路径

Python Sqlite3获取Sqlite连接路径,python,sqlite,Python,Sqlite,给定sqlite3连接对象,如何检索sqlite3文件的文件路径?不存储此信息 您可以在打开连接之前存储路径: path = '/path/to/database/file.db' conn = sqlite3.connect(path) 或者,您可以使用以下命令询问数据库本身有哪些连接: 如果使用了(使用sqlite3.connect()参数uri=True),文件名将不包括uri参数或文件://前缀。我们可以使用PRAGMA database\u list命令 cur = con.curs

给定sqlite3连接对象,如何检索sqlite3文件的文件路径?

不存储此信息

您可以在打开连接之前存储路径:

path = '/path/to/database/file.db'
conn = sqlite3.connect(path)
或者,您可以使用以下命令询问数据库本身有哪些连接:


如果使用了(使用
sqlite3.connect()
参数
uri=True
),文件名将不包括uri参数或
文件://
前缀。

我们可以使用PRAGMA database\u list命令

cur = con.cursor()
cur.execute("PRAGMA database_list")
rows = cur.fetchall()

for row in rows:
    print(row[0], row[1], row[2])
第三个参数(第[2]行)是数据库的文件名。 请注意,可能有更多数据库连接到SQLite引擎

$ ./list_dbs.py 
0 main /home/user/dbs/test.db
2 movies /home/user/dbs/movies.db

以上是包含Python代码的脚本的示例输出。

引用Martijn Pieters,除了必须进行硬编码外,您应该执行以下操作:

path = os.path.dirname(os.path.abspath(__file__))
db = os.path.join(path, 'file.db')

conn = sqlite3.connect(db)

这个答案是有效的,应该被接受。
path = os.path.dirname(os.path.abspath(__file__))
db = os.path.join(path, 'file.db')

conn = sqlite3.connect(db)