"Database is locked" with SQLite and C#

BlackAlpha picture BlackAlpha · Jul 5, 2016 · Viewed 10.5k times · Source

I have a problem with my code. I want to delete some rows in my SQLite database but I get a "Database is locked" exception. I read several posts about that like this post, but my problem is still here.

Here is my code :

using (var c = new SQLiteConnection(_connectionSQLite))
{
    c.Open();

    if (c.State == ConnectionState.Open)
    {
        var reqExist = string.Concat("SELECT id FROM ... ");
        using (var cmdExist = new SQLiteCommand(reqExist, c))
        {
            var reqUpdate = string.Concat("UPDATE ... WHERE id = ", cmdExist.ExecuteScalar());

            using (var cmdUpdate = new SQLiteCommand(reqUpdate, c))
            {
                cmdUpdate.ExecuteNonQuery();
            }
        }
    }

    c.Close();
}

I get the "database is locked" exception on the line cmdUpdate.ExecuteNonQuery();. I tried replace with a DELETE FROM, same result, but with a SELECT, it works, I really don't understand what's wrong with my code.

Thanks for any help.

Answer

BlackAlpha picture BlackAlpha · Jul 5, 2016

Ok everyone, thanks for your help. I just have to .dispose() two readers in an other function before insert/update, and it works !