can't delete a row from a SQL table with c# function

I have a function to delete rows from a table in a SQLite database. Function doesn't throw an exception or crash but it also fails to delete anything. "cmd_1.ExecuteNonQuery()" returns zero but the row I am trying to delete exists. What am I doing wrong here?

        public void deleteEntryInTable(string tableName, string pKeyName, string pKeyValue)
    {
        conDB.Open();
        cmd_1 = conDB.CreateCommand();
        cmd_1.CommandText = $"DELETE FROM '{tableName}' WHERE '{pKeyName}'='{pKeyValue}'";
        System.Diagnostics.Debug.WriteLine(cmd_1.CommandText);
        try
        {
            int rows = cmd_1.ExecuteNonQuery();
            System.Diagnostics.Debug.WriteLine(rows.ToString());
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.ToString());
        }
        conDB.Close();
    }

Solution 1:

To expand on what Haldo said; don't ever surround identifiers (table names, column names) with single quotes. SQLite does support it, in that if you use it somewhere that an identifier is expected you don't get a complaint, but in SQL single quotes are for denoting strings. I think it highly likely that your where clause is being treated like WHERE 'a' = 'b' and "string a equals string b" is always false.

See this fiddle: https://dbfiddle.uk/?rdbms=sqlite_3.27&fiddle=3b8c9364bedb03138403fa741485779a

The first query selects a result, the second does not


Other things to consider if you're in a "why isn't my database changing?" scenario

  • SQLite is a file based database. Are you absolutely sure you're looking in the same db file that your code is altering? If I had a dollar for every time that a dev has been working with a file based db, looking in the db in c:\temp\my.db and going "why can't I see the changes??" not realizing that the program is editing the file in c:\repos\myproject\bin\debug\my.db I'd retire

  • The other thing; are you absolutely sure that the id you're quoting truly exists in the db? plenty of times I've run a delete, then come to run the same delete again but second time round there is nothing left to delete so the table row count doesn't change..


Lastly, as a few people have commented, don't make SQL like you have done. Prefer a form like:

public void DeleteEntryInTable(string tableName, string pKeyName, string pKeyValue)
{
    conDB.Open();
    cmd_1 = conDB.CreateCommand();
    cmd_1.CommandText = $"DELETE FROM {tableName} WHERE {pKeyName} = @pKeyValue";
    cmd_1.Parameters.AddWithValue("@pKeyValue", pKeyValue);
    System.Diagnostics.Debug.WriteLine(cmd_1.CommandText);
    try
    {
        int rows = cmd_1.ExecuteNonQuery();
        System.Diagnostics.Debug.WriteLine(rows.ToString());
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine(ex.ToString());
    }
    conDB.Close();
}

Don't ever, ever allow a user to supply data for table name or column name; they may only ever supply a value for pKeyValue, for example picking which record to delete off a drop down list in a webpage. If you switch to using SQLServer db in future, avoid AddWithValue

It actually looks like you're trying to write something to make your database life easier; have you considered learning something like Entity Framework?