当前位置:首页 > 数据库 > SQlite

如何在Python中锁定sqlite3数据库?

有没有办法在Python中显式获取sqlite3数据库的锁?

解决方法:

显式锁定数据库的方法是启动事务,如documentation中所述:

When a database is accessed by multiple connections, and one of the processes modifies the database, the SQLite database is locked until that transaction is committed.

启动交易的一种方法是使用connection as a context manager

import sqlite3
con = sqlite3.connect(...)
...
with con:
    # Database is locked here

另请注意,某些事务默认情况下发生在implictly

By default, the sqlite3 module opens transactions implicitly before a Data Modification Language (DML) statement (i.e. INSERT/UPDATE/DELETE/REPLACE), and commits transactions implicitly before a non-DML, non-query statement (i. e. anything other than SELECT or the aforementioned).


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!