Locking a sqlite3 database in Python (re-asking for clarification)

前端 未结 2 663
名媛妹妹
名媛妹妹 2020-12-05 20:43

A few weeks ago, I posted this question on SO regarding how to lock a sqlite3 database in python:

How to lock a sqlite3 database in Python?

However, I\'m not

2条回答
  •  我在风中等你
    2020-12-05 21:06

    'with con' is NOT what is wanted here. (or this thread locking rubbish)

    To get exclusive access for a specific period (not just while an individual query/trasaction is taking place) you need to do;

    con = sqlite3.connect()
    con.isolation_level = 'EXCLUSIVE'
    con.execute('BEGIN EXCLUSIVE')
    #exclusive access starts here. Nothing else can r/w the db, do your magic here.
    con.commit()
    con.close()
    

    Hopefully this saves someone from the searching/experimenting i've just been through!

    Remember it's not exclusive until you run begin exclusive, and it will stay exclusive until you close (or run commit, i think). You can always test w/ the python interpreter / CL sqlite3 app if you aren't sure.

提交回复
热议问题