正在打开没有写访问权限的sqlite3数据库

2024-06-26 02:10:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从Python对SQLite数据库执行查询。问题是,我没有对数据库文件的写入权限,也没有对包含数据库文件的目录的访问权限。在

当我连接到数据库并执行SELECT查询时,我得到一个“无法打开数据库文件”错误。我试着听从建议,但没用。我猜SQLite在尝试创建锁文件时失败了。在

当我有对目录的写访问权限,但没有对sqlite文件的写访问权限时,我会遇到另一个错误-锁定错误。这是因为sqlite创建shm和wal文件的权限与db文件相同,这意味着我得到了无法写入的shm和wal文件,从而导致锁定错误。在

除了将所有文件复制到我有完全访问权限的目录之外,还有其他方法吗?在


Tags: 文件方法目录数据库权限dbsqlite错误
2条回答

您可以使用以下语法在读取模式下打开sqlite3连接:

con = sqlite3.connect('file:path/to/database.sqlite?mode=ro', uri=True)

documentation上写着:

It is not possible to open read-only WAL databases. The opening process must have write privileges for "-shm" wal-index shared memory file associated with the database, if that file exists, or else write access on the directory containing the database file if the "-shm" file does not exist.

若要允许对该数据库的只读访问,某些具有写权限的用户需要将其更改为另一个journal mode。在

相关问题 更多 >