简体   繁体   English

SQLite数据库在执行更改表查询时被锁定C#

[英]SQLite database is locked on executing alter table query c#

The following function is used for the newer version of my app which needs to add a column to the existing database. 以下功能用于我的应用程序的较新版本,需要将列添加到现有数据库中。

public void AddColumnIfNotexist()
    {
      try
      {
       using (SQLiteCommand cmd = _DBConnection.CreateCommand())
       {
         cmd.CommandText = string.Format("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'myTable'");
         bool hascol = false;
         using (SQLiteDataReader reader = cmd.ExecuteReader())
         {

           if (reader.Read())
           {
             //does column exists?
              hascol = reader.GetString(0).Contains(String.Format("\"{0}\"", "myNewColumn"));
             reader.Close();

            }
         }
               if (!hascol)
               {
                 StringBuilder sql = new StringBuilder(SQLMAXLENGTH);
                 sql.Append("ALTER TABLE Groups ADD COLUMN IsStayingRatio BIT NOT NULL DEFAULT 0");
                  cmd.CommandText = sql.ToString();
                  cmd.ExecuteNonQuery();


                  }
                    }
                }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "\r\n\r\n" + ex.StackTrace);
                LogApp.Write("Exception ex:" + ex.Message +"stacktrace "+ex.StackTrace, LogApp.Level.Error);

            }

        }
    }

When I execute this gets an exception as 当我执行时会得到一个异常

database is locked 数据库已锁定

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(CommandBehavior behavior)
at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery()
at DataPlus.DB.AddColumnIfNotexist() in D:\Projects\DB.cs:line 343

But this is not always happened. 但这并不总是发生。 this happens if the size of the DB Fil is large. 如果DB Fil的大小很大,则会发生这种情况。 Also, the issue is not there while debugging from the IDE. 另外,从IDE调试时也不存在问题。

Is there any limitations for the Alter Table query? 更改表查询有任何限制吗? Can anyone spot out the issue which makes the DB locked? 谁能找出导致数据库锁定的问题?

I would suggest to start a new SQLiteCommand 我建议开始一个新的SQLiteCommand

Note that an SQLITE_LOCKED error is distinct from SQLITE_BUSY (5). 请注意,SQLITE_LOCKED错误与SQLITE_BUSY(5)不同。 SQLITE_BUSY means that another database connection (probably in another process) is using the database in a way that prevents you from using it. SQLITE_BUSY表示另一个数据库连接(可能在另一个进程中)正在使用某种方式阻止您使用该数据库。 SQLITE_LOCKED means the source of contention is internal and comes from the same database connection that received the SQLITE_LOCKED error. SQLITE_LOCKED意味着争用源是内部的,并且来自接收到SQLITE_LOCKED错误的同一数据库连接。

Sometimes people think they have finished with a SELECT statement because sqlite3_step() has returned SQLITE_DONE. 有时人们认为他们已经完成了SELECT语句,因为sqlite3_step()返回了SQLITE_DONE。 But the SELECT is not really complete until sqlite3_reset() or sqlite3_finalize() have been called 但是,直到调用sqlite3_reset()或sqlite3_finalize()时,SELECT才真正完成

Error Code SQLITE_LOCKED (6): Database Is Locked 错误代码SQLITE_LOCKED(6):数据库已锁定

Adding the following lines after reader.Close() worked for me. reader.Close()之后添加以下几行对我reader.Close()

GC.Collect();
GC.WaitForPendingFinalizers();

It seems that the DB was not released on executing the ALTER query. 似乎在执行ALTER查询时未释放数据库。 Got the hint from here . 这里得到了提示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM