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

SQLite返回以下错误:

SQL error: database is locked

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


当前回答

我也有同样的问题。显然,回滚函数似乎用与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 )

其他回答

一个老问题,有很多答案,这里是我最近遵循的步骤,阅读上面的答案,但在我的情况下,问题是由于cifs资源共享。这个病例以前没有报道过,希望能帮助到一些人。

检查java代码中没有任何连接是打开的。 使用lsof检查是否有其他进程正在使用您的SQLite db文件。 检查正在运行的jvm进程的用户所有者对该文件具有r/w权限。 尝试在连接开口上强制锁定模式 final SQLiteConfig config = new SQLiteConfig(); config.setReadOnly(假); config.setLockingMode (LockingMode.NORMAL); connection = DriverManager。getConnection (url, config.toProperties ());

如果您在NFS共享文件夹上使用SQLite db文件,请检查SQLite faq的这一点,并检查您的挂载配置选项,以确保避免锁定,如下所述:

//myserver /mymount cifs username=*****,password=*****,iocharset=utf8,sec=ntlm,file,nolock,file_mode=0700,dir_mode=0700,uid=0500,gid=0500 0 0

这是因为该数据库上正在运行其他一些查询。SQLite是一个同步执行查询的数据库。如果其他人正在使用该数据库,那么如果你执行查询或事务,它就会给出这个错误。

因此,停止正在使用特定数据库的进程,然后执行查询。

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

使用Linux shell将是:

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

从您之前的评论中,您说存在一个-journal文件。

这可能意味着您已经打开和(EXCLUSIVE?)事务,还没有提交数据。是你的程序或者其他进程留下了-journal吗?

重新启动sqlite进程将查看日志文件,清除任何未提交的操作并删除-journal文件。

如果文件位于远程文件夹(如共享文件夹)中,则可能引发此错误。我将数据库更改为本地目录,它工作得很好。