Ram Prasad

Ram Prasad

  • NA
  • 326
  • 18.4k

breaking sqlite loop and getting error of database being locked

Sep 23 2021 1:33 AM

I am using code below to enter data into sqllite database if it does not exist in it. Also, if availalbe, retrieving information on where it is stored. I used "break" to get out once data is found and perform the same action with new value of bItems. However, it is giving me error of System.Data.SQLite.SQLiteException: 'database is locked database is locked' @ code line cmd1.ExecuteNonQuery();. My understanding is I am getting it somehow due to "break". Can someone explain how can I fix this error. Thanks

{
    
    for (int p = 0; p < 256; p++)
    {
        bItems += "P" + buffer[p];                                   
    }
    
    using (SQLiteConnection con = new SQLiteConnection(databaseObject.myConnection)) 
    {
        con.Open();
        SQLiteCommand cmd = new SQLiteCommand("select ID, Data from B where Data like 'P%'", con);
        var rdr = cmd.ExecuteReader();
        while (rdr.Read())
        {
            if (Convert.ToString(rdr["Data"]) != bItems)
            {
                SQLiteCommand cmd1 = new SQLiteCommand("INSERT INTO B ('Data') SELECT @Data  WHERE NOT EXISTS (SELECT ID, Data FROM B WHERE  Data = @Data)", con);
                cmd1.Parameters.AddWithValue("@Data", bItems);
                cmd1.ExecuteNonQuery(); // GETTING ERROR HERE
            }
            else
            {
                sItems = "B" + Convert.ToString(rdr["ID"]);
                con.Close();
                break;
            }
        }
    }
    bItems = "";
    Console.WriteLine(sItems);
}

Answers (2)