Python SQLite: database is locked

后端 未结 20 1153
一生所求
一生所求 2020-12-12 14:24

I\'m trying this code:

import sqlite

connection = sqlite.connect(\'cache.db\')
cur = connection.cursor()
cur.execute(\'\'\'create table item
  (id integer p         


        
相关标签:
20条回答
  • 2020-12-12 14:57

    Easy solution: check once if you have opened the database in another window or in another terminal. That also locks your database. In my case, I closed all the other terminals that were locking the database (a terminal tab in Pycharm). Check each tab of the terminals of your IDE as well if there is a terminal that left the database open. exit() all the terminals should work unlocking the database.

    0 讨论(0)
  • 2020-12-12 15:01

    Turned out the problem happened because the path to the db file was actually a samba mounted dir. I moved it and that started working.

    0 讨论(0)
  • 2020-12-12 15:04

    I had the same problem: sqlite3.IntegrityError

    As mentioned in many answers, the problem is that a connection has not been properly closed.

    In my case I had try except blocks. I was accessing the database in the try block and when an exception was raised I wanted to do something else in the except block.

    try:
        conn = sqlite3.connect(path)
        cur = conn.cursor()
        cur.execute('''INSERT INTO ...''')
    except:
        conn = sqlite3.connect(path)
        cur = conn.cursor()
        cur.execute('''DELETE FROM ...''')
        cur.execute('''INSERT INTO ...''')
    

    However, when the exception was being raised the connection from the try block had not been closed.

    I solved it using with statements inside the blocks.

    try:
        with sqlite3.connect(path) as conn:
            cur = conn.cursor()
            cur.execute('''INSERT INTO ...''')
    except:
        with sqlite3.connect(path) as conn:
            cur = conn.cursor()
            cur.execute('''DELETE FROM ...''')
            cur.execute('''INSERT INTO ...''')
    
    0 讨论(0)
  • 2020-12-12 15:05

    I also had this problem. I was trying to enter data into the database without saving changes I had made in it. after i saved the changes worked

    0 讨论(0)
  • 2020-12-12 15:06

    The reason mine was showing the "Lock" message was actually due to me having opened an SQLite3 IDE on my mac and that was the reason it was locked. I assume I was playing around with the DB within the IDE and hadn't saved the changes and therefor a lock was placed.

    Cut long story short, check that there are no unsaved changes on the db and also that it is not being used elsewhere.

    0 讨论(0)
  • 2020-12-12 15:06

    in my case ,the error happened when a lot of concurrent process trying to read/write to the same table. I used retry to workaround the issue

    def _retry_if_exception(exception):
        return isinstance(exception, Exception)
    
    @retry(retry_on_exception=_retry_if_exception,
           wait_random_min=1000,
           wait_random_max=5000,
           stop_max_attempt_number=5)
    def execute(cmd, commit=True):
       c.execute(cmd)
       c.conn.commit()
    
    0 讨论(0)
提交回复
热议问题