You can't do that.
You have the connection open to handle the reader in the outer
while
loop, and you are trying to UPDATE the same table from the same connection within that loop - these operations are incompatible, so you get a "Database is locked" error.
The best solution would be to dump the Reader, and load a DataTable via a DataAdapter instead, and then use the DataTable data to loop and do the UPDATE statements.
"Well, is not about I give up, I'm sick of this update :)) I tried to change the code in so many ways, but without success. This is my last try. I get the same error. I tried to close and reopen connection, than close is again, but with no succees. I tried with DataAdapter, with DataReader but it didn't work. I don't know to else to try. And I have to change only a value from a single row, don't understand why to use foreach loop.."
Why a foreach loop? Because a query of any form returns a number of rows - which may be zero (if there is no match), one (if there is only one), or more (if there is more than one value) - and it's alwasy good practice to not assume that it will return one and only one, even if your program logic says it will. Data errors happen! :laugh:
So start by using a DataAdapter instead of the DataReader:
string collect = "SELECT option1,option2,option3 FROM questions WHERE question=@QU";
SQLiteCommand comp = new SQLiteCommand(collect, Conexiune.getConnection());
comp.CommandType = CommandType.Text;
comp.Parameters.AddWithValue("@QU", SimulatorManager.Intrebare);
SQLiteDataAdapter da = new SQLiteDataAdapter(comp);
DataTable dt = new DataTable();
da.Fill(dt);
foreach (DataRow row in dt.Rows)
{
...
}
(I've made some changes to the way you did it: SQL verbs are normally in UPPERCASE to make it easier to pick them out of an SQL statement. And never, ever concatenate strings to form an SQL statement - that way leads to SQL injection attacks which can damage or destroy your database, just by typing in text boxes.
Always use parameterized queries!)
Then in the loop you just use the row info to decide if an update is needed:
int op1 = (int)row["option1"];
int op2 = (int)row["option2"];
int op3 = (int)row["option3"];
...
Make sense so far?