[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 512
  • Last Modified:

Delphi 7 - What's wrong with this code? - Trying to delete an SQL row.

The code below is used when someone focus on a TreeList and chooses the popup menu 'Delete'.

procedure TfrmMain.DeleteTask;
var
Query: TADOQuery;
LId: String;
begin
  //Grabs the ID of the selected row
  if Assigned(cxTasks.FocusedNode) then
   LId := cxTasks.FocusedNode.Texts[cxTasksID.ItemIndex];

  //Attempts to delete that row
  try
    Query := TADOQuery.Create(nil);
    Query.Connection := conSql;
    Query.SQL.Add('Select * FROM Tasks WHERE id='+LId); 
    Query.Active := True;
    Query.Delete;
    Query.Active := False;
    qryTasks.Refresh; //Refresh 
    //Query.SQL.Add('DELETE FROM Tasks WHERE id='+LId); 
    //Query.ExecSQL;
  finally
    FreeAndNil(Query);
  end;
end;

Open in new window


However, when I run the code I get this error message:
 SQL
Any idea what's wrong here?

Thanks.
0
John86a
Asked:
John86a
2 Solutions
 
Ephraim WangoyaCommented:

Use the code you disabled but use quotedstr
procedure TfrmMain.DeleteTask;
var
  Query: TADOQuery;
  LId: String;
begin
  //Grabs the ID of the selected row
  if Assigned(cxTasks.FocusedNode) then
   LId := cxTasks.FocusedNode.Texts[cxTasksID.ItemIndex];

  //Attempts to delete that row
  Query := TADOQuery.Create(nil);
  try    
    Query.Connection := conSql;
    Query.SQL.Add(Format('DELETE FROM Tasks WHERE id = %s', [QuotedStr(LId)]); 
    Query.ExecSQL;

    qryTasks.Refresh; //Refresh 
  finally
    FreeAndNil(Query);
  end;
end;

Open in new window

0
 
Ephraim WangoyaCommented:

I think I missed one bracket

procedure TfrmMain.DeleteTask;
var
  Query: TADOQuery;
  LId: String;
begin
  //Grabs the ID of the selected row
  if Assigned(cxTasks.FocusedNode) then
   LId := cxTasks.FocusedNode.Texts[cxTasksID.ItemIndex];

  //Attempts to delete that row
  Query := TADOQuery.Create(nil);
  try    
    Query.Connection := conSql;
    Query.SQL.Add(Format('DELETE FROM Tasks WHERE id = %s', [QuotedStr(LId)]));
    Query.ExecSQL;

    qryTasks.Refresh; //Refresh
  finally
    FreeAndNil(Query);
  end;
end;
0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
I guess that the error comes from the qryTasks.refresh
Use qryTasks.requery instead
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jimyXCommented:
What is the datatype of the column "ID" in the table "Tasks"?
procedure TfrmMain.DeleteTask;
var
  Query: TADOQuery;
  LId: String;
begin
  //Grabs the ID of the selected row
  if Assigned(cxTasks.FocusedNode) then
   LId := cxTasks.FocusedNode.Texts[cxTasksID.ItemIndex];

  //Attempts to delete that row
  try
    Query := TADOQuery.Create(nil);
    Query.Connection := conSql;
    Query.Active := False;
    Query.SQL.Text := 'DELETE FROM Tasks WHERE id=:TID';
    Query.ParamByName('TID').Value:=LId;
    Query.ExecSQL;
    qryTasks.Refresh; //Refresh 
  finally
    FreeAndNil(Query);
  end;
end;

Open in new window

0
 
John86aAuthor Commented:
Indeed, the error was due to Refresh, Requery solved it.
Thanks.
0
 
FrozeniceCommented:
hi,


its like deleting a certain row that is not existing anymore.

and why need to select?

you can simply do..


 try
    Query := TADOQuery.Create(nil);
    Query.Connection := conSql;
    Query.SQL.Add('DELETE FROM Tasks WHERE id='+LId);
    Query.ExecSQL;
  finally
    FreeAndNil(Query);
  end;






BR
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now