当我输入这个查询: 删除邮件中id = 71的所有邮件

SQLite返回以下错误:

SQL error: database is locked

我如何解锁数据库,以便这个查询将工作?


当前回答

我的锁是由系统崩溃引起的,而不是由挂起进程引起的。为了解决这个问题,我简单地重命名了文件,然后将其复制回原来的名称和位置。

使用Linux shell将是:

mv mydata.db temp.db
cp temp.db mydata.db

其他回答

SQLite db文件只是文件,所以第一步是确保它不是只读的。另一件要做的事情是确保在DB打开时没有某种GUI SQLite DB查看器。可以在另一个shell中打开DB,也可以在代码中打开DB。通常情况下,如果不同的线程或应用程序(如SQLite Database Browser)打开了数据库以便写入,则会看到这种情况。

由于某种原因,数据库被锁定了。以下是我的解决方法。

我将sqlite文件下载到我的系统(FTP) 删除在线sqlite文件 将文件上传到主机提供商

现在可以正常工作了。

我只是犯了同样的错误。 5分钟后,我发现我没有关闭一个shell巫婆正在使用db。 请关闭它,再试一次;)

在我的例子中,我也得到了这个错误。

我已经检查了其他进程,可能是锁定数据库的原因,如(SQLite管理器,连接到我的数据库的其他程序)。但是没有其他程序连接到它,它只是同一个应用程序中保持连接的另一个活动SQLConnection。

在建立新的SQLConnection和新命令之前,尝试检查您以前的活动SQLConnection,它可能仍然被连接(首先断开它)。

我也有同样的问题。显然,回滚函数似乎用与db文件相同但没有最近更改的日志覆盖了db文件。我已经在下面的代码中实现了这一点,从那时起它一直工作得很好,而之前我的代码会因为数据库保持锁定而陷入循环。

希望这能有所帮助

我的python代码

##############
#### 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 )