更新语句无法正常工作。的SQL [英] Update statement not working correctly. SQL

查看:121
本文介绍了更新语句无法正常工作。的SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

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;

我的问题是,每当我单击此按钮时,它的确会计数(特定的行和列投票设置为默认值0,每次我单击按钮时,它都必须加上spinedit值和投票列的值。)

My problem is that it does count up each time I click this button (The specific row and column "Votes" is set to defualt value of 0, every time I click on the button it must plus the spinedit value with the value of the column "Votes".)

现在使用此代码,它只是替换值。
我怎么了?

Now with this code it is just replacing the values. What did I do wrong ?

预先感谢。

推荐答案

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

This would be clearer, safer, and faster if you use parameters. Also, you can refer to the old column value right in the query, rather than getting the old value from your current query.

尝试以下操作:

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;

这篇关于更新语句无法正常工作。的SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆