SQLite数据库锁定异常 [英] SQLite Database Locked exception

查看:284
本文介绍了SQLite数据库锁定异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到的数据库被锁定的SQLite 例外,只是一些查询。

下面是我的code: 当我执行任何select语句它工作正常。
当我在工作执行任何写语句表也能正常工作。

这工作得很好:

 的ExecuteNonQuery(DELETE FROM乔布斯WHERE ID = 1);
 

但是,如果我执行查询员工表则抛出异常以同样的方式数据库被锁定
该抛出异常:

 的ExecuteNonQuery(DELETE FROM雇员WHERE ID = 1);
 

下面是我的功能:

 公共BOOL OpenConnection的()
        {
            如果(续== NULL)
            {
                CON =新SQLiteConnection(的ConnectionString);
            }
            如果(Con.State == ConnectionState.Closed)
            {
                Con.Open();
                // CMD =新SQLiteCommand(杂FOREIGN_KEYS = ON,刀豆);
                //Cmd.ExecuteNonQuery();
                //Cmd.Dispose();
                // CMD = NULL;
                返回true;
            }
            如果(IsConnectionBusy())
            {
                Msg.Log(新的异常(连接忙));
            }
            返回false;
        }


        公布尔CloseConnection()
        {
            如果(CON =空&放大器;!&安培; Con.State == ConnectionState.Open)
            {
                如果(!CMD = NULL)Cmd.Dispose();
                CMD = NULL;
                Con.Close();
                返回true;
            }

            返回false;
        }



        公共布尔的ExecuteNonQuery(SQL字符串)
        {
            如果(SQL == NULL)返回false;
            尝试
            {
                如果(!OpenConnection的())
                    返回false;
                其他
                {
                    //的Tx = Con.BeginTransaction(IsolationLevel.ReadCommitted);
                    CMD =新SQLiteCommand(SQL,刀豆​​);
                    Cmd.ExecuteNonQuery();
                    //Tx.Commit();
                    返回true;
                }
            }
            赶上(例外的例外)
            {
                //Tx.Rollback();
                Msg.Log(例外);
                返回false;
            }
            最后
            {
                CloseConnection();
            }
        }
 

这是个例外: 在103行: Cmd.ExecuteNonQuery();

  

异常发现:         类型:System.Data.SQLite.SQLiteException        消息:数据库已锁定       数据库已锁定        资料来源:System.Data.SQLite

     

堆栈跟踪:在System.Data.SQLite.SQLite3.Step(SQLiteStatement语句)          在System.Data.SQLite.SQLiteDataReader.NextResult()          在System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand CMD,循规蹈矩的CommandBehavior)          在System.Data.SQLite.SQLiteCommand.ExecuteReader(的CommandBehavior行为)          在System.Data.SQLite.SQLiteCommand.ExecuteNonQuery()          在TimeSheet6.DbOp.ExecuteNonQuery(SQL字符串)在d:\项目\ C#应用程序\竣工项目\ TimeSheet6 \ TimeSheet6 \ DbOp.cs:103行

解决方案

某处的连接得到保持打开状态的方式。摆脱 OpenConnection的 CloseConnection 和变化的ExecuteNonQuery

 使用(SQLiteConnection C =新SQLiteConnection(的ConnectionString))
{
    c.Open();
    使用(SQLiteCommand CMD =新SQLiteCommand(SQL,C))
    {
        cmd.ExecuteNonQuery();
    }
}
 

此外,改变你的的方式读数据这样:

 使用(SQLiteConnection C =新SQLiteConnection(的ConnectionString))
{
    c.Open();
    使用(SQLiteCommand CMD =新SQLiteCommand(SQL,C))
    {
        使用(SQLiteDataReader RDR = cmd.ExecuteReader())
        {
            ...
        }
    }
}
 

不要尝试,以管理连接在自己的汇集喜欢你在这里。首先,它要复杂得多比你有codeD,但第二个,它的 SQLiteConnection 对象内部处理了。最后,如​​果你不利用使用,您的不处理这些对象正常,你最终喜欢你所看到的问题现在。

I am getting Database is locked exception from SQLite for some queries only.

Below is my code: When I execute any select statement it works fine.
When I am executing any write statement on Jobs Table it also works fine..

This works fine:

ExecuteNonQuery("DELETE FROM Jobs WHERE id=1");

But the same way if I am executing queries for Employees table it is throwing exception that database is locked.
This throws Exception:

ExecuteNonQuery("DELETE FROM Employees WHERE id=1");

Below are my functions:

        public bool OpenConnection()
        {
            if (Con == null)
            {
                Con = new SQLiteConnection(ConnectionString);
            }
            if (Con.State == ConnectionState.Closed)
            {
                Con.Open();
                //Cmd = new SQLiteCommand("PRAGMA FOREIGN_KEYS=ON", Con);
                //Cmd.ExecuteNonQuery();
                //Cmd.Dispose();
                //Cmd=null;
                return true;
            }
            if (IsConnectionBusy())
            {
                Msg.Log(new Exception("Connection busy"));
            }
            return false;
        }


        public Boolean CloseConnection()
        {
            if (Con != null && Con.State == ConnectionState.Open)
            {
                if (Cmd != null) Cmd.Dispose();
                Cmd = null;
                Con.Close();
                return true;
            }

            return false;
        }



        public Boolean ExecuteNonQuery(string sql)
        {
            if (sql == null) return false;
            try
            {
                if (!OpenConnection())
                    return false;
                else
                {
                    //Tx = Con.BeginTransaction(IsolationLevel.ReadCommitted);
                    Cmd = new SQLiteCommand(sql, Con);
                    Cmd.ExecuteNonQuery();
                    //Tx.Commit();
                    return true;
                }
            }
            catch (Exception exception)
            {
                //Tx.Rollback();
                Msg.Log(exception);
                return false;
            }
            finally
            {
                CloseConnection();
            }
        }

This is the Exception: At line 103 : Cmd.ExecuteNonQuery();

Exception Found: Type: System.Data.SQLite.SQLiteException Message: database is locked database is locked Source: System.Data.SQLite

Stacktrace: at System.Data.SQLite.SQLite3.Step(SQLiteStatement stmt) at System.Data.SQLite.SQLiteDataReader.NextResult() at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave) at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior) at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery() at TimeSheet6.DbOp.ExecuteNonQuery(String sql) in d:\Projects\C# Applications\Completed Projects\TimeSheet6\TimeSheet6\DbOp.cs:line 103

解决方案

Somewhere along the way a connection is getting left open. Get rid of OpenConnection and CloseConnection and change ExecuteNonQuery to this:

using (SQLiteConnection c = new SQLiteConnection(ConnectionString))
{
    c.Open();
    using (SQLiteCommand cmd = new SQLiteCommand(sql, c))
    {
        cmd.ExecuteNonQuery();
    }
}

Further, change the way you read data to this:

using (SQLiteConnection c = new SQLiteConnection(ConnectionString))
{
    c.Open();
    using (SQLiteCommand cmd = new SQLiteCommand(sql, c))
    {
        using (SQLiteDataReader rdr = cmd.ExecuteReader())
        {
            ...
        }
    }
}

Do not attempt, to manage connection pooling on your own like you are here. First, it's much more complex than what you have coded, but second, it's handled already inside the SQLiteConnection object. Finally, if you're not leveraging using, you're not disposing these objects properly and you end up with issues like what you're seeing now.

这篇关于SQLite数据库锁定异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆