I have this helper function:
public bool Delete(String tableName, String where)
{
Boolean returnCode = true;
try
{
this.ExecuteNonQuery(String.Format("delete from {0} where {1};", tableName, where));
}
catch (Exception fail)
{
MessageBox.Show(fail.Message);
returnCode = false;
}
return returnCode;
}
TableName contains “[MyTable]”
and where contains “[MyTable ID]=’4ffbd580-b17d-4731-b162-ede8d698e026′” which is a unique guid representing the row ID.
The function returns true, like it was successful, and no exception, but the rows are not deleted from DB, what’s wong?
This is the ExecuteNonQuery function
public int ExecuteNonQuery(string sql)
{
SQLiteConnection cnn = new SQLiteConnection(dbConnection);
cnn.Open();
SQLiteCommand mycommand = new SQLiteCommand(cnn);
mycommand.CommandText = sql;
int rowsUpdated = mycommand.ExecuteNonQuery();
cnn.Close();
return rowsUpdated;
}
Firstly, you shouldn’t just embed SQL like that. You should use parameterized SQL, to avoid SQL injection attacks.
Next, you should look at the return value of
ExecuteNonQuery. Assuming it follows the normal pattern ofExecuteNonQuery, it should return the number of rows affected. I suspect it’s returning 0, which will mean that nothing’s matching yourwhereclause.(I’d also stop catching
Exception– probably stop catching anything, but at least catch something specific if you must. I’d get rid of the local variable, too, and just return directly when you know what you want to return…)