Python Sqlite3 获取Sqlite连接路径 [英] Python Sqlite3 Get Sqlite Connection path

查看:79
本文介绍了Python Sqlite3 获取Sqlite连接路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个sqlite3连接对象,如何获取sqlite3文件的文件路径?

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

推荐答案

Python 连接对象 不存储此信息.

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

You could store the path before you open the connection:

path = '/path/to/database/file.db'
conn = sqlite3.connect(path)

或者您可以使用database_list编译指示:

or you can ask the database itself what connections it has, using the database_list pragma:

for id_, name, filename in conn.execute('PRAGMA database_list'):
    if name == 'main' and filename is not None:
        path = filename
        break

如果您使用了连接URI(使用sqlite3.connect() 参数 uri=True),文件名将不包含 URI 参数或 file:// 前缀.

If you used a connection URI (connecting with the sqlite3.connect() parameter uri=True), the filename will not include the URI parameters or the file:// prefix.

这篇关于Python Sqlite3 获取Sqlite连接路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆