gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 06:32:04 48 4
gpt4 key购买 nike

有没有办法在 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).

关于python - 如何在 Python 中锁定 sqlite3 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8828495/

48 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com