C# C SQLite删除不起作用
我有一个助手函数:C# C SQLite删除不起作用,c#,sqlite,C#,Sqlite,我有一个助手函数: public bool Delete(String tableName, String where) { Boolean returnCode = true; try { this.ExecuteNonQuery(String.Format("delete from {0} where {1};", tableName, where)); }
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包含[MyTable]
其中包含[MyTable ID]=“4ffbd580-b17d-4731-b162-ede8d698e026”,这是表示行ID的唯一guid
函数返回true,就像它成功了一样,并且没有异常,但是没有从数据库中删除行,这是什么
这是ExecuteOnQuery函数
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;
}
首先,您不应该像那样嵌入SQL。您应该使用参数化SQL,以避免SQL注入攻击 接下来,您应该查看ExecuteOnQuery的返回值。假设它遵循ExecuteOnQuery的正常模式,它应该返回受影响的行数。我怀疑它返回0,这意味着没有任何内容与where子句匹配
我也会停止捕捉异常——可能会停止捕捉任何东西,但如果必须的话,至少要捕捉一些特定的东西。我也会去掉局部变量,当您知道要返回什么时直接返回…我在Web服务器上不使用SQLite,这是一个Windows应用程序,ExecuteOnQuery将0作为受影响的行返回,为什么?行的ID是一种GUID类型,而不是StringAlway,至少可以将SQL查询连接到它自己的变量中,这样您就可以在将查询发送到数据库之前检查查询。您可以将相同的查询粘贴到任何SQLite数据库浏览器中,Firefox插件相当不错。这应该会让事情变得显而易见。我已经在SQLite数据库浏览器2.0中粘贴了查询并执行了它。返回消息没有错误,也没有删除任何行。它似乎无法从我的字符串中找到guid类型列