更新语句无法正常工作。 SQL

时间:2013-09-05 21:48:42

标签: delphi ms-access ado

这是我的代码:

procedure TForm4.BitBtn1Click(Sender: TObject);    
var      
    spinval, iVotes: integer;    
begin    
    if DBLookupComboBox1.Text = ' ' then    
    begin    
        ShowMessagePos('Please select a candidate.', 1000, 500);    
    end    
    else    
    begin    
        spinval := SpinEdit1.value;    
        ADOQuery1.Active := false;    
        ADOQuery1.SQL.Text := 'Update Candidate_table set votes = ''' +
            inttostr(spinval + Candidatetable.fieldbyname('Votes').AsInteger) + 
            ''' where Name = ''' + DBLookupComboBox1.Text + '''';    
        ADOQuery1.ExecSQL;    
        ADOQuery1.Active := false;    
        ADOQuery1.SQL.Text := 'Select * from Candidate_table';    
        ADOQuery1.Active := true;    
        MessageDlgPos('Thank you for voting. You will be logged out.' , mtInformation, [mbOK], 0, 1000, 500);    
        Form4.Hide;    
        Form2.Show;    
    end    
end;

我的问题是,每次单击此按钮时它都会计数(特定的行和列“Votes”设置为defualt值为0,每次单击按钮时它必须加上带有值的spinedit值“投票”栏目。)

现在使用此代码只是替换值。 我做错了什么?

提前感谢。

1 个答案:

答案 0 :(得分:3)

如果使用参数,这将更清晰,更安全,更快捷。此外,您可以在查询中引用旧列值,而不是从当前查询中获取旧值。

试试这个:

ADOQuery1.SQL.Text := 
    'Update Candidate_table set votes = votes + :number where Name = :candidate';
ADOQuery1.Parameters.ParamByName('number').Value := spinval;
ADOQuery1.Parameters.ParamByName('candidate').Value := DBLookupComboBox1.Text;
ADOQuery1.ExecSQL;
相关问题