以下方法应检查表中是否已存在记录或否。但是,我收到语法错误" 没有这样的列"。
public void ifExist(int myId)
{
string dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "ormdemo.db3");
var db = new SQLiteConnection(dbPath);
SQLiteCommand cmd = new SQLiteCommand(db);
cmd.CommandText = "SELECT count(*) FROM storeConsumption WHERE Id = ?"+ myId;
int count = Convert.ToInt32(cmd.ExecuteScalar<storeConsumption>());
if (count == 0)
{
Console.WriteLine("The record is NOT Existed");
}
else
{
Console.WriteLine("The record is Existed");
}
}
虽然,仍有同样的错误。如果你知道如何解决它,我将感激不尽?
更新:我可以像这样解决这个问题:
public async Task<Boolean> ifExist(int id){
var result = await sdb.databaseConnection().ExecuteScalarAsync<int>("SELECT count(*) FROM storeConsumption WHERE Id = ?", id);
Console.WriteLine(result);
if (result > 0) return true;
return false;
}
答案 0 :(得分:1)
似乎由“Id =?”引起。如果要添加参数,请尝试使用以下命令行
cmd.CommandText =“SELECT count(*)FROM storeConsumption WHERE Id = @myId”; cmd.Parameters.Add( “@ MYID”,MYID);
更多信息: https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.commandtext(v=vs.110).aspx
答案 1 :(得分:0)
更新:以下代码已经解决了错误并且运行良好:
public async Task<Boolean> ifExist(int id){
var result = await sdb.databaseConnection().ExecuteScalarAsync<int>("SELECT count(*) FROM storeConsumption WHERE Id = ?", id);
Console.WriteLine(result);
if (result > 0) return true;
return false;
}