2017-08-24 72 views
-1

以下方法,应该检查记录是否已经存在于表中或者没有。但是,我收到语法错误没有这样的列。sqlite异常没有这样的列

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"); 
    } 
}  

我也试着像这样运行:

cmd.CommandText = "SELECT count(*) FROM storeConsumption WHERE Id= ?'"+ myId+"'"; 

虽然,仍然有同样的错误。如果你有一个想法,我该如何解决它,我会感激不尽。

} 
+0

卸载应用程序再次intall检查一次。 –

回答

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

+0

感谢John的回复。 Visual Studio无法识别Add方法。实际上,我只用了4种方法用于我的cmd.Paramater。虽然,我添加了所有必要的库。你有什么建议吗? –

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; 

}