Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

delphi update problem 1

Status
Not open for further replies.

neoreturned

Programmer
Apr 25, 2006
9
TR
procedure TForm1.Button2Click(Sender: TObject);
begin
Query1.Active:=False;
Query1.SQL.Add('UPDATE UYELER SET UyeAdi='''+Edit2.Text+''' WHERE UyeKodu='''+Edit1.Text+'''');
Query1.Open;
end;

end.

when i run this procedure it gives an error like: "Invalid use token: UPDATE" why? also how i update my query wtih edit boxes??
 
The best way to do this sort of thing is to use parameters in your query. Your query would look like this:
Code:
UPDATE UYELER SET UyeAdi=:Param2 WHERE UyeKodu=:Param1
If you can use this exact same query over and over, you don't have to set the SQL in your code - you put it in the TQuery at design time. Then go to the Params property of the TQuery and set the DataType (ftString) and ParamType (ptInput) of the two params.

Your code would then look like this:
Code:
procedure TForm1.Button2Click(Sender: TObject);
begin
    Query1.Active:=False;
    Query1.ParamByName('Param1').AsString := Edit1.Text;
    Query1.ParamByName('Param2').AsString := Edit2.Text;
    Query1.Open;
end;
This is MUCH more efficient on the database, because the query only has to be "prepared" once with a parameterized query. It also prevents SQL Injection type attacks on your database because the database will only parse values in the parameters and not SQL.

If you have to reset the SQL in your code, you would also have to set the DataType and ParamType in your code prior to setting the value of the parameter.

-Dell

A computer only does what you actually told it to do - not what you thought you told it to do.
 
I think you are forgetting to clear the SQL before you add another line into it.

You either use
Query1.SQL.Text:='UPDATE UYELER SET UyeAdi='''+Edit2.Text+''' WHERE UyeKodu='''+Edit1.Text+'''';

or Clear the SQL before adding again
Query1.SQL.Clear;
Query1.SQL.Add('UPDATE UYELER SET UyeAdi='''+Edit2.Text+''' WHERE UyeKodu='''+Edit1.Text+'''');

otherwise you are adding too many update lines in your query


Code:
procedure TForm1.Button2Click(Sender: TObject);
begin
    Query1.Active:=False;
    Query1.SQL.Clear; //i think you are forgetting this
    Query1.SQL.Add('UPDATE UYELER SET UyeAdi='''+Edit2.Text+''' WHERE UyeKodu='''+Edit1.Text+'''');
    Query1.Open;
end;

hope it helps
 
I'm pretty sure that any action queries (UPDATE, INSERT) need to be called with EXECSQL not open.

I would also recommend using the QuotedStr function to replace all the ''' ''' that makes things harder to read:

Code:
procedure TForm1.Button2Click(Sender: TObject);
begin
  with Query1 do
  begin
    Active:=False;
    SQL.Clear;
    SQL.Add('UPDATE UYELER SET UyeAdi=' + QuotedStr(Edit2.Text) + ' WHERE UyeKodu= ' + QuotedStr(Edit1.Text) );
    ExecSQL;
  end;
end;

Leslie

Anything worth doing is a lot more difficult than it's worth - Unknown Induhvidual

Essential reading for anyone working with databases: The Fundamentals of Relational Database Design
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top