breaking sqlite loop and getting error of database being locked

aks sha 6 Reputation points
2021-09-23T00:56:42.663+00:00

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);
}
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,811 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,308 questions
{count} vote