简体   繁体   English

无法使用 c# function 从 SQL 表中删除一行

[英]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 从 SQLite 数据库中的表中删除行。 Function doesn't throw an exception or crash but it also fails to delete anything. Function 不会引发异常或崩溃,但它也无法删除任何内容。 "cmd_1.ExecuteNonQuery()" returns zero but the row I am trying to delete exists. “cmd_1.ExecuteNonQuery()”返回零,但我试图删除的行存在。 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();
    }

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. SQLite 确实支持它,因为如果您在需要标识符的地方使用它,您不会收到投诉,但在 SQL 中,单引号用于表示字符串。 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.我认为您的 where 子句很可能被视为WHERE 'a' = 'b'并且“字符串 a 等于字符串 b”总是错误的。

See this fiddle: https://dbfiddle.uk/?rdbms=sqlite_3.27&fiddle=3b8c9364bedb03138403fa741485779a看到这个小提琴: 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. SQLite 是一个基于文件的数据库。 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??"如果每次开发人员使用基于文件的数据库时我都有一美元,请查看 c:\temp\my.db 中的数据库,然后“为什么我看不到更改??” not realizing that the program is editing the file in c:\repos\myproject\bin\debug\my.db I'd retire没有意识到程序正在编辑 c:\repos\myproject\bin\debug\my.db 中的文件我会退休

  • The other thing;另一件事; are you absolutely sure that the id you're quoting truly exists in the db?您绝对确定您引用的 id 确实存在于数据库中吗? 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.最后,正如一些人所评论的那样,不要像你所做的那样制作 SQL。 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.他们可能只为 pKeyValue 提供一个值,例如从网页的下拉列表中选择要删除的记录。 If you switch to using SQLServer db in future, avoid AddWithValue如果您将来切换到使用 SQLServer db,请避免使用 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?您是否考虑过学习实体框架之类的东西?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM