How do I unlock a SQLite database?

前端 未结 30 1410
栀梦
栀梦 2020-11-22 15:56
sqlite> DELETE FROM mails WHERE (`id` = 71);
SQL error: database is locked

How do I unlock the database so this will work?

相关标签:
30条回答
  • 2020-11-22 16:39

    This link solve the problem. : When Sqlite gives : Database locked error It solved my problem may be useful to you.

    And you can use begin transaction and end transaction to not make database locked in future.

    0 讨论(0)
  • 2020-11-22 16:40

    I had the same problem. Apparently the rollback function seems to overwrite the db file with the journal which is the same as the db file but without the most recent change. I've implemented this in my code below and it's been working fine since then, whereas before my code would just get stuck in the loop as the database stayed locked.

    Hope this helps

    my python code

    ##############
    #### Defs ####
    ##############
    def conn_exec( connection , cursor , cmd_str ):
        done        = False
        try_count   = 0.0
        while not done:
            try:
                cursor.execute( cmd_str )
                done = True
            except sqlite.IntegrityError:
                # Ignore this error because it means the item already exists in the database
                done = True
            except Exception, error:
                if try_count%60.0 == 0.0:       # print error every minute
                    print "\t" , "Error executing command" , cmd_str
                    print "Message:" , error
    
                if try_count%120.0 == 0.0:      # if waited for 2 miutes, roll back
                    print "Forcing Unlock"
                    connection.rollback()
    
                time.sleep(0.05)    
                try_count += 0.05
    
    
    def conn_comit( connection ):
        done        = False
        try_count   = 0.0
        while not done:
            try:
                connection.commit()
                done = True
            except sqlite.IntegrityError:
                # Ignore this error because it means the item already exists in the database
                done = True
            except Exception, error:
                if try_count%60.0 == 0.0:       # print error every minute
                    print "\t" , "Error executing command" , cmd_str
                    print "Message:" , error
    
                if try_count%120.0 == 0.0:      # if waited for 2 miutes, roll back
                    print "Forcing Unlock"
                    connection.rollback()
    
                time.sleep(0.05)    
                try_count += 0.05       
    
    
    
    
    ##################
    #### Run Code ####
    ##################
    connection = sqlite.connect( db_path )
    cursor = connection.cursor()
    # Create tables if database does not exist
    conn_exec( connection , cursor , '''CREATE TABLE IF NOT EXISTS fix (path TEXT PRIMARY KEY);''')
    conn_exec( connection , cursor , '''CREATE TABLE IF NOT EXISTS tx (path TEXT PRIMARY KEY);''')
    conn_exec( connection , cursor , '''CREATE TABLE IF NOT EXISTS completed (fix DATE, tx DATE);''')
    conn_comit( connection )
    
    0 讨论(0)
  • 2020-11-22 16:40

    I got this error in a scenario a little different from the ones describe here.

    The SQLite database rested on a NFS filesystem shared by 3 servers. On 2 of the servers I was able do run queries on the database successfully, on the third one thought I was getting the "database is locked" message.

    The thing with this 3rd machine was that it had no space left on /var. Everytime I tried to run a query in ANY SQLite database located in this filesystem I got the "database is locked" message and also this error over the logs:

    Aug 8 10:33:38 server01 kernel: lockd: cannot monitor 172.22.84.87

    And this one also:

    Aug 8 10:33:38 server01 rpc.statd[7430]: Failed to insert: writing /var/lib/nfs/statd/sm/other.server.name.com: No space left on device Aug 8 10:33:38 server01 rpc.statd[7430]: STAT_FAIL to server01 for SM_MON of 172.22.84.87

    After the space situation was handled everything got back to normal.

    0 讨论(0)
  • 2020-11-22 16:41

    the SQLite db files are just files, so the first step would be to make sure it isn't read-only. The other thing to do is to make sure that you don't have some sort of GUI SQLite DB viewer with the DB open. You could have the DB open in another shell, or your code may have the DB open. Typically you would see this if a different thread, or application such as SQLite Database Browser has the DB open for writing.

    0 讨论(0)
  • 2020-11-22 16:41

    My lock was caused by the system crashing and not by a hanging process. To resolve this I simply renamed the file then copied it back to it's original name and location.

    Using a linux shell that would be...

    mv mydata.db temp.db
    cp temp.db mydata.db
    
    0 讨论(0)
  • 2020-11-22 16:44

    I added "Pooling=true" to connection string and it worked.

    0 讨论(0)
提交回复
热议问题