1

The Votes column defualt value is 0. Every time when I click the button it must add whichever value I want to the specific row I want.

My error is :

Syntax error : Update statement. [[Delphi]]

This is my code :

procedure TForm4.BitBtn1Click(Sender: TObject);    
var     
  spinval : integer;
begin
  spinval := SpinEdit1.value;``

  // Candidatetable.Insert;
  // Candidatetable['Votes'] := Candidatetable['Votes'] + spinval;

  ADOQuery1.Active := false;
  ADOQuery1.SQL.Text := 'Update Candidate_table set votes = ''' +
                        Candidatetable['Votes'] + IntToStr(spinval) +
                        ''' 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;

PlEASE HELP =)

Thanks.

10
  • 3
    Use named parameters instead of unsanitized string concatenation. Commented Sep 5, 2013 at 12:34
  • maybe remove the tick marks after` spinval := SpinEdit1.value;` Commented Sep 5, 2013 at 12:49
  • 1
    Which part of the task are you having trouble with? Look at your SQL and find the error. You haven't shown the SQL here. You've shown the formula you use to construct the SQL, but not the result, and the result is obviously the part that Delphi dislikes. Commented Sep 5, 2013 at 13:19
  • 4
    Jaco, please stop what you are doing right now and google the words "SQL injection". Commented Sep 5, 2013 at 13:42
  • 1
    @Jaco: I've rolled back (reversed) your last edit. It is not allowed here to change the entire question after you've received answers to it. Doing so can make answers no longer match the question, causing downvotes. It's also a big waste of the time of people who answered your first question only to have you change it completely. If you have a new question, post a new question. You might want to read How to Ask again, which you agreed to understanding when you created your account here. Commented Sep 5, 2013 at 21:36

1 Answer 1

4

I think this is what you are looking for.

  ADOQuery1.SQL.Clear;
  ADOQuery1.SQL.Add('Update Candidate_table');
  ADOQuery1.SQL.Add('set votes = votes + :Votes');
  ADOQuery1.SQL.Add('where Name = :Name');

  ADOQuery1.Parameters[0].Value := spinval;
  ADOQuery1.Parameters[1].Value := DBLookupComboBox1.Text;
Sign up to request clarification or add additional context in comments.

8 Comments

Small remark, I would use ParamByName, not magic index values.
@whosrdaddy, magic is faster, non-magic safer :-) Yes, in this case it definitely doesn't matter, but for repetitive parameter assignments I'd either store TParameter reference or use index.
I tend to use index values when the sql is right close to the parameter setting line. If the Sql is elsewhere I use ParamByName unless I am looking for something VERY performant.
Uhg...Sorry its not working it isnt counting the votes up as i like its just replacing the votes' value to the spinedit's value
@Jaco: just replace the line ADOQuery1.SQL.Add('set votes = :Votes'); with ADOQuery1.SQL.Add('set votes = votes + :Votes');
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.