2017-10-05 37 views
0
iexc := round(iexc/icount); 
    iint := round(iint/icount); 
    inau := round(inau/icount); 
    iovr := round(iovr/icount); 

    adoquery1.SQL.Text := 'update Tickets_Tbl set Excitment = iexc where 
    RollerCoaster = "'+sride+'"'; 
    adoquery1.ExecSQL; 
    adoquery1.SQL.Text := 'update Tickets_Tbl set Intensity = iint where 
    RollerCoaster = "'+sride+'"'; 
    adoquery1.ExecSQL; 
    adoquery1.SQL.Text := 'update Tickets_Tbl set Nausea = inau where 
    RollerCoaster = "'+sride+'"'; 
    adoquery1.ExecSQL; 
    adoquery1.SQL.Text := 'update Tickets_Tbl set Overall = iovr where 
    RollerCoaster = "'+sride+'"'; 
    adoquery1.ExecSQL; 

主要問題是,當我運行代碼時,它一直說沒有字段開始更新的默認值。請幫忙。使用Delphi 10在MS Access中使用整型變量更新整型值

+2

不知道德爾福,但我猜(根據你的其他線),'adoquery1.SQL.Text:='更新Tickets_Tbl設置Excitment = iexc其中'應該是'adoquery1.SQL.Text:='更新Tickets_Tbl set Excitment ='+ iexc +'where'(即你想從你的程序中插入值到SQL語句中) – YowE3K

+1

問題是你用常量字符串'iexc'而不是那個變量。但使用參數。 – Victoria

回答

1

問題是你在你的SQL中硬編碼變量名,而不是正確地設置值(這應該使用參數化查詢,順便說一句)。另外,你的SQL很可怕;你可以在一個UPDATE中做到這一點,而不是多次重複。

這應該讓你開始(見下注意有關#13):

iexc := round(iexc/icount); 
iint := round(iint/icount); 
inau := round(inau/icount); 
iovr := round(iovr/icount); 

AdoQuery1.SQL.Text := 'update Tickets_Tbl'#13 + 
         'set Excitement = :iexc,'#13 + 
         ' Intensity = :iint,'#13 + 
         ' Nausea = :inau,'#13 + 
         ' Overall = :iovr'#13 + 
         'where RollerCoaster = :sride'; 
AdoQuery1.Parameters.ParamByName('iexc').Value := iexc; 
AdoQuery1.Parameters.ParamByName('iint').Value := iint; 
AdoQuery1.Parameters.ParamByName('inau').Value := inau; 
AdoQuery1.Parameters.ParamByName('iovr').Value := iovr; 
AdoQuery1.Parameters.ParamByName('sride').Value := sride; 
AdoQuery1.ExecSQL; 

在代碼#13是一個回車,這只是使得它更容易,因爲你不必有關正確擔心在每個連續行結束或開始之前或之後插入空格。這與將查詢逐行輸入到SQL Server Management Studio中基本相同,請按在每行末尾輸入。對於每一行,您都可以使用AdoQuery1.SQL.Add來做同樣的事情,但是對於我來說,當您需要複製SQL並將其清理乾淨以便在外部運行以進行測試或調試或修改時,這會讓您更難。以上內容類似於

AdoQuery1.SQL.Clear; 
AdoQuery1.SQL.Add('update Tickets_Tbl'); 
AdoQuery1.SQL.Add('set Excitement = :iexc,'); 
AdoQuery1.SQL.Add('Intensity = :iint,'); 
AdoQuery1.SQL.Add('Nausea = :inau,'); 
AdoQuery1.SQL.Add('Overall = :iovr'); 
AdoQuery1.SQL.Add('where RollerCoaster = :sride'); 

使用無論哪一個更容易,更易於個人閱讀。我使用第一種,因爲我有一個實用工具,它將從剪貼板中取出普通的SQL,並自動將其格式化爲'#13 +以粘貼到Delphi的代碼編輯器中,或者將包含嵌入的''字符和#13 +的引用文本自動移至允許粘貼到SSMS或Access或其他SQL實用程序而無需手動清理。

+0

爲什麼#13而不是#13#10? – kobik

+0

@ kobik:因爲似乎沒有必要同時使用兩者。使用該工具可以正常工作,無論是在上面顯示的程序代碼中還是在我的實用程序將代碼轉換回純文本時。爲什麼在不需要時在每一行輸入額外的#10? :-)感謝編輯,順便說一句。良好的發現 - 我校對時錯過了。 –

+0

謝謝,稍後會檢查是否有效。 –