3 Replies - 939 Views - Last Post: 17 August 2013 - 09:13 PM Rate Topic: -----

#1 Dylan1234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-August 13

I want to increase a value in a database using sql in delphi.

Posted 14 August 2013 - 05:02 AM

Hey guys,
i am doing a project for my school using delphi where we have to create a voting system etc.
i want to increase the number of votes for a certain category using Sql in delphi.
so i have a variable for the category already set,
here is my Code

QryVotes.Active := False;
QryVotes.SQL.Text := 'Update Votes SET '+Cata+' = ' + Cata+ ' + 1 WHERE Band_ID = 1 ';
qryVotes.ExecSql ;

Band_Id is the Id of the band i will be voting for, it gives me a syntax error when i run it like this.
Any Suggestions or help will be appreciated :)
(i am new to sql)

Is This A Good Question/Topic? 0
  • +

Replies To: I want to increase a value in a database using sql in delphi.

#2 DouglasNewton  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 11-August 13

Re: I want to increase a value in a database using sql in delphi.

Posted 14 August 2013 - 11:55 AM

I don't think you can do that in SQL. Break it into steps.
1) Get the number of votes the band has now, and store it in x.
2) Increase x by 1.
3) Do an UPDATE query that sets the number of votes that the band has to the new value of x.

So it's be something like
SELECT NumVotes FROM Bands WHERE BandName='Korn'



And then
UPDATE Bands SET NumVotes = [put x into string here] WHERE BandName = 'Korn'



I'm not entirely sure about my syntax, but the idea's there. Hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3615
  • View blog
  • Posts: 12,438
  • Joined: 12-December 12

Re: I want to increase a value in a database using sql in delphi.

Posted 14 August 2013 - 12:12 PM

You can do this in a single statement:

QryVotes.SQL.Text := 'Update Votes SET Cata = Cata + 1 WHERE Band_ID = 1';

I assume Band_ID is the name of your field and the value 1 is just for testing. In reality, you are likely to substitute a variable for this number 1:

QryVotes.SQL.Text := 'Update Votes SET Cata = Cata + 1 WHERE Band_ID = ' + somevar;

Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1369
  • View blog
  • Posts: 4,737
  • Joined: 19-February 09

Re: I want to increase a value in a database using sql in delphi.

Posted 17 August 2013 - 09:13 PM

Does += work?

QryVotes.SQL.Text := 'Update Votes SET ' + Cata + ' += 1 WHERE Band_ID = 1 ';


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1