2017-08-24 102 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; 

}