SQLite 显示 "Insufficient Parameters supplied to the command" 尽管有足够的参数
SQLite showing "Insufficient Parameters supplied to the command" despite having enough parameters
我有以下 SQLite table。
CREATE TABLE "Ingredient_Detailed" (
"DETAILED_INGREDIENT_ID" TEXT,
"INGREDIENT_CODE" INTEGER NOT NULL,
"BRAND" TEXT NOT NULL,
"INGREDIENT_SOURCE" TEXT NOT NULL,
"UNIT_PRICE" REAL NOT NULL DEFAULT 0,
"AMOUNT_IN_UNIT" REAL NOT NULL DEFAULT 0,
"MINIMUM_PRICE_PER_UNIT" REAL NOT NULL DEFAULT 0,
"QUALITY" INTEGER NOT NULL DEFAULT 1,
"UNITS_AVAILABLE" INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY("INGREDIENT_CODE") REFERENCES "Ingredient"("CODE"),
PRIMARY KEY("DETAILED_INGREDIENT_ID")
)
我有一个 C# 应用程序,我试图使用以下方法将记录插入此 table:
public int SaveDetailedIngredient(DetailedIngredient pDetailedIngredient)
{
try
{
using (var conn = new SQLiteConnection(GetConnectionString()))
{
var saveDetailedIngredientCommand = new SQLiteCommand("INSERT INTO INGREDIENT_DETAILED (DETAILED_INGREDIENT_ID, " +
"INGREDIENT_CODE, BRAND, INGREDIENT_SOURCE, UNIT_PRICE, AMOUNT_IN_UNIT, " +
"MINIMUM_PRICE_PER_UNIT, QUALITY, UNITS_AVAILABLE) " +
"VALUES ($pDetailedIngredientId, $pCode, $pBrand, $pSource, $pUnitPrice, $pAmountInUnit, $pPricePerUnit, $pQuality, $pUnitsAvailable)", conn);
pDetailedIngredient.DetailedIngredientCode = pDetailedIngredient.Code + "-" + pDetailedIngredient.Brand + "-" + pDetailedIngredient.IngredientSource;
saveDetailedIngredientCommand.Parameters.AddWithValue("pDetailedIngredientId", pDetailedIngredient.DetailedIngredientCode);
saveDetailedIngredientCommand.Parameters.AddWithValue("pCode", pDetailedIngredient.Code);
saveDetailedIngredientCommand.Parameters.AddWithValue("pBrand", pDetailedIngredient.Brand.Trim().ToUpper());
saveDetailedIngredientCommand.Parameters.AddWithValue("pSource", pDetailedIngredient.IngredientSource.Trim().ToUpper());
saveDetailedIngredientCommand.Parameters.AddWithValue("pUnitPrice,", pDetailedIngredient.UnitPrice);
saveDetailedIngredientCommand.Parameters.AddWithValue("pAmountInUnit", pDetailedIngredient.AmountInUnit);
saveDetailedIngredientCommand.Parameters.AddWithValue("pPricePerUnit", pDetailedIngredient.MinimumUnitPrice);
saveDetailedIngredientCommand.Parameters.AddWithValue("pQuality", pDetailedIngredient.Quality);
saveDetailedIngredientCommand.Parameters.AddWithValue("pUnitsAvailable", pDetailedIngredient.UnitsAvailable);
conn.Open();
return saveDetailedIngredientCommand.ExecuteNonQuery();
}
}
catch (SQLiteException sqlEx)
{
Console.WriteLine(sqlEx.Message);
Console.WriteLine(sqlEx.ErrorCode);
throw sqlEx;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw ex;
}
}
尽管指示了 9 个字段并提供了 9 个参数,SQLite 还是抛出了一个异常,提示“未知错误:向命令提供的参数不足”。
我复制并粘贴了参数名称以确保其中没有拼写错误,但它仍然会引发错误。
我在方法执行期间调试了应用程序,提供的 pDetailedIngredient
分配了所有必要的属性值,我可以看到命令中的每个参数都被正确分配。
我还有其他几种向其他 table 插入数据的方法,都遵循相同的结构,所以我怀疑这与我的方法的编写方式有关。
我是不是漏掉了什么?感觉这不是正确的错误。
老实说,我希望我能很好地回答“为什么”会发生这种情况。我只能假设“AddWithValue”正在做我不知道的事情。然而,经过一些来回我会请你试试这个,看看它是否对你有用。
就是我改的是添加如下所示的参数。这始终适用于一些测试,并且仅在重复 id 时才按预期失败。请让我知道这是否对我有帮助,因为从表面上看,您的代码对我来说看起来不错。
saveDetailedIngredientCommand.Parameters.Add("pDetailedIngredientId", DbType.String).Value = pDetailedIngredient.DetailedIngredientCode;
saveDetailedIngredientCommand.Parameters.Add("pCode", DbType.Int32).Value = pDetailedIngredient.Code;
saveDetailedIngredientCommand.Parameters.Add("pBrand", DbType.String).Value = pDetailedIngredient.Brand.Trim().ToUpper();
saveDetailedIngredientCommand.Parameters.Add("pSource", DbType.String).Value = pDetailedIngredient.IngredientSource.Trim().ToUpper();
saveDetailedIngredientCommand.Parameters.Add("pUnitPrice", DbType.Decimal).Value = pDetailedIngredient.UnitPrice;
saveDetailedIngredientCommand.Parameters.Add("pAmountInUnit", DbType.Decimal).Value = pDetailedIngredient.AmountInUnit;
saveDetailedIngredientCommand.Parameters.Add("pPricePerUnit", DbType.Decimal).Value = pDetailedIngredient.MinimumUnitPrice;
saveDetailedIngredientCommand.Parameters.Add("pQuality", DbType.Int32).Value = pDetailedIngredient.Quality;
saveDetailedIngredientCommand.Parameters.Add("pUnitsAvailable", DbType.Int32).Value = pDetailedIngredient.UnitsAvailable;
如果这对您不起作用,请告诉我,我会删除它。
我有以下 SQLite table。
CREATE TABLE "Ingredient_Detailed" (
"DETAILED_INGREDIENT_ID" TEXT,
"INGREDIENT_CODE" INTEGER NOT NULL,
"BRAND" TEXT NOT NULL,
"INGREDIENT_SOURCE" TEXT NOT NULL,
"UNIT_PRICE" REAL NOT NULL DEFAULT 0,
"AMOUNT_IN_UNIT" REAL NOT NULL DEFAULT 0,
"MINIMUM_PRICE_PER_UNIT" REAL NOT NULL DEFAULT 0,
"QUALITY" INTEGER NOT NULL DEFAULT 1,
"UNITS_AVAILABLE" INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY("INGREDIENT_CODE") REFERENCES "Ingredient"("CODE"),
PRIMARY KEY("DETAILED_INGREDIENT_ID")
)
我有一个 C# 应用程序,我试图使用以下方法将记录插入此 table:
public int SaveDetailedIngredient(DetailedIngredient pDetailedIngredient)
{
try
{
using (var conn = new SQLiteConnection(GetConnectionString()))
{
var saveDetailedIngredientCommand = new SQLiteCommand("INSERT INTO INGREDIENT_DETAILED (DETAILED_INGREDIENT_ID, " +
"INGREDIENT_CODE, BRAND, INGREDIENT_SOURCE, UNIT_PRICE, AMOUNT_IN_UNIT, " +
"MINIMUM_PRICE_PER_UNIT, QUALITY, UNITS_AVAILABLE) " +
"VALUES ($pDetailedIngredientId, $pCode, $pBrand, $pSource, $pUnitPrice, $pAmountInUnit, $pPricePerUnit, $pQuality, $pUnitsAvailable)", conn);
pDetailedIngredient.DetailedIngredientCode = pDetailedIngredient.Code + "-" + pDetailedIngredient.Brand + "-" + pDetailedIngredient.IngredientSource;
saveDetailedIngredientCommand.Parameters.AddWithValue("pDetailedIngredientId", pDetailedIngredient.DetailedIngredientCode);
saveDetailedIngredientCommand.Parameters.AddWithValue("pCode", pDetailedIngredient.Code);
saveDetailedIngredientCommand.Parameters.AddWithValue("pBrand", pDetailedIngredient.Brand.Trim().ToUpper());
saveDetailedIngredientCommand.Parameters.AddWithValue("pSource", pDetailedIngredient.IngredientSource.Trim().ToUpper());
saveDetailedIngredientCommand.Parameters.AddWithValue("pUnitPrice,", pDetailedIngredient.UnitPrice);
saveDetailedIngredientCommand.Parameters.AddWithValue("pAmountInUnit", pDetailedIngredient.AmountInUnit);
saveDetailedIngredientCommand.Parameters.AddWithValue("pPricePerUnit", pDetailedIngredient.MinimumUnitPrice);
saveDetailedIngredientCommand.Parameters.AddWithValue("pQuality", pDetailedIngredient.Quality);
saveDetailedIngredientCommand.Parameters.AddWithValue("pUnitsAvailable", pDetailedIngredient.UnitsAvailable);
conn.Open();
return saveDetailedIngredientCommand.ExecuteNonQuery();
}
}
catch (SQLiteException sqlEx)
{
Console.WriteLine(sqlEx.Message);
Console.WriteLine(sqlEx.ErrorCode);
throw sqlEx;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw ex;
}
}
尽管指示了 9 个字段并提供了 9 个参数,SQLite 还是抛出了一个异常,提示“未知错误:向命令提供的参数不足”。
我复制并粘贴了参数名称以确保其中没有拼写错误,但它仍然会引发错误。
我在方法执行期间调试了应用程序,提供的 pDetailedIngredient
分配了所有必要的属性值,我可以看到命令中的每个参数都被正确分配。
我还有其他几种向其他 table 插入数据的方法,都遵循相同的结构,所以我怀疑这与我的方法的编写方式有关。
我是不是漏掉了什么?感觉这不是正确的错误。
老实说,我希望我能很好地回答“为什么”会发生这种情况。我只能假设“AddWithValue”正在做我不知道的事情。然而,经过一些来回我会请你试试这个,看看它是否对你有用。
就是我改的是添加如下所示的参数。这始终适用于一些测试,并且仅在重复 id 时才按预期失败。请让我知道这是否对我有帮助,因为从表面上看,您的代码对我来说看起来不错。
saveDetailedIngredientCommand.Parameters.Add("pDetailedIngredientId", DbType.String).Value = pDetailedIngredient.DetailedIngredientCode;
saveDetailedIngredientCommand.Parameters.Add("pCode", DbType.Int32).Value = pDetailedIngredient.Code;
saveDetailedIngredientCommand.Parameters.Add("pBrand", DbType.String).Value = pDetailedIngredient.Brand.Trim().ToUpper();
saveDetailedIngredientCommand.Parameters.Add("pSource", DbType.String).Value = pDetailedIngredient.IngredientSource.Trim().ToUpper();
saveDetailedIngredientCommand.Parameters.Add("pUnitPrice", DbType.Decimal).Value = pDetailedIngredient.UnitPrice;
saveDetailedIngredientCommand.Parameters.Add("pAmountInUnit", DbType.Decimal).Value = pDetailedIngredient.AmountInUnit;
saveDetailedIngredientCommand.Parameters.Add("pPricePerUnit", DbType.Decimal).Value = pDetailedIngredient.MinimumUnitPrice;
saveDetailedIngredientCommand.Parameters.Add("pQuality", DbType.Int32).Value = pDetailedIngredient.Quality;
saveDetailedIngredientCommand.Parameters.Add("pUnitsAvailable", DbType.Int32).Value = pDetailedIngredient.UnitsAvailable;
如果这对您不起作用,请告诉我,我会删除它。