【问题标题】:SqLite Update ErrorSqlite 更新错误
【发布时间】:2014-02-02 19:06:08
【问题描述】:

我想知道这段代码有什么问题。 我得到了问题:“,”附近的错误语法错误

它接受一个项目代码和一个项目对象。

public override int Update(string ItemCode, Item Item)
{
    int rc = 0;

    try
    {
        _sqlConnection = new SQLiteConnection(_connStr);
        _sqlConnection.Open();

我认为问题在于下一条语句:

        string updateQuery = string.Format("UPDATE ItemDatabase SET _itemDescription=@ItemDescription, _ItemPrice=@ItemPrice, _ItemName=@ItemName WHERE [_itemDescription] = '{0}' AND [_itemPrice] = '{1}' AND [_itemName] = '{2}'", Item.ItemDescription, Item.ItemPrice, Item.ItemName);

我不确定是否应该用逗号分隔。

        SQLiteCommand sqlCommand = new SQLiteCommand(updateQuery, _sqlConnection);

        SQLiteParameter[] sqlParams = new SQLiteParameter[]
            {
            new SQLiteParameter("@ItemDescription",DbType.String),
            new SQLiteParameter("@ItemPrice",DbType.String),
            new SQLiteParameter("@ItemName", DbType.String)
            }; // end SQLiteParameter
        sqlParams[0].Value = Item.ItemDescription;
        sqlParams[1].Value = Item.ItemPrice;
        sqlParams[2].Value = Item.ItemName;
        sqlCommand.Parameters.AddRange(sqlParams);
        rc = sqlCommand.ExecuteNonQuery();
        if (rc == 1)
        {
            rc = 0;
        }
        _sqlConnection.Close();
    }
    catch (SQLiteException ex)
    {
        throw ex;
    }
    catch (Exception ex)
    {

        throw ex;
    }
    return rc;
}

【问题讨论】:

  • 调试代码时看到的updateQuery
  • 为什么要在字段前面加上下划线?
  • 这里有些地方不太对劲。用于参数的值与用于查询的值相同。所以你没有找到任何东西,或者你将字段更新为相同的值
  • 在表格中我的字段名称用下划线命名。我在他们的代码中加入了“and”并删除了逗号,然后它给了我这个消息:Insufficient parameters provided to the command
  • 我现在更改了代码,它说项目更新但数据没有改变

标签: c# sqlite insert sql-update


【解决方案1】:

您的更新查询如下。

string updateQuery = string.Format(@"UPDATE ItemDatabase 
      SET _itemDescription=@ItemDescription, 
      _ItemPrice=@ItemPrice, 
      _ItemName=@ItemName 
      WHERE [_itemDescription] = '{0}', 
            [_itemPrice] = '{1}', 
            [_itemName] = '{2}'", Item.ItemDescription, Item.ItemPrice, Item.ItemName);

所以你的参数应该被调用

   SQLiteParameter[] sqlParams = new SQLiteParameter[]
   {
        new SQLiteParameter("@ItemDescription",DbType.String),
        new SQLiteParameter("@ItemPrice",DbType.String),
        new SQLiteParameter("@ItemName", DbType.String)
    }; 

当然,WHERE 语句的正确语法要求当您有多个条件时,您可以使用 AND 或 OR 运算符将它们连接起来

      WHERE [_itemDescription] = '{0}' AND 
            [_itemPrice] = '{1}' AND 
            [_itemName] = '{2}'", Item.ItemDescription, Item.ItemPrice, Item.ItemName);

但是,正如我在上面的评论中所问的那样,您对参数集合和 where 子句使用相同的值。如果您使用相同的值,则不会对数据库应用任何更改。

我举个例子:

  Item.Description = "Item1";
  Item.ItemPrice = "100";
  Item.ItemName = "Stuff";

您的查询变为:

string updateQuery = @"UPDATE ItemDatabase 
      SET _itemDescription='Item1', 
      _ItemPrice='100', 
      _ItemName='Stuff' 
      WHERE [_itemDescription] = 'Item1' AND 
            [_itemPrice] = '100' AND 
            [_itemName] = 'Stuff'");

因此,即使找到记录也不会更改您的数据

【讨论】:

  • 我将参数从“@_itemDescription”更改为“@ItemDescription”,将“@_itemPrice”更改为“@ItemPrice”,将“@_itemName”更改为“@ItemName”,这就是你所说的值是一样的
【解决方案2】:

where condition, condition, condition 不是有效的 SQL。你的意思要么是where condition and condition and condition,要么是where condition or condition or condition

此外,您可以摆脱那些除了重置异常堆栈跟踪之外什么都不做的catch 块。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多