Question

When trying to execute the code:

function TDBClass.addNome(nome: String): String;
var
  rsnome: TADOQuery;
begin
  rsnome := TADOQuery.Create(nil);
  rsnome.Connection := connection;
  rsnome.Open();
  rsnome.SQL.Clear;
  rsnome.SQL.Text:='UPDATE enroll SET nome = "test" where id ="1"';
  rsnome.Parameters.ParamByName('nome').Value:= nome;
  rsnome.ExecSQL;
  rsnome.post();
  rsnome.Close();
  rsnome.Free();
end;

I'm receiving the error message "Missing SQL property". Where did I go wrong?
Thanks in advance!

Was it helpful?

Solution

I don't think you want to use Open at all, and you are using parameters incorrectly.
i.e. the SQL doesn't have any :PARAM placeholders in it. I think it should be something like: rsnome.SQL.Text:='UPDATE enroll SET nome = :NOME where id = :ID';

See this example: AdoQuery Error using parameters

OTHER TIPS

You're calling rsnome.Open before setting the SQL by rsnome.SQL.Text := ....

You have several errors. You're calling Open before you've assigned ths SQL (and without needing to do so).

You're trying to set a parameter value you haven't created a parameter to accept. (BTW, I'd make ID also a parameter, so you can use this to update more than one person's name.)

You're not handling making sure that things get cleaned up in case there's an error (which given the code you posted there certainly will be).

You're using Post, which isn't necessary with an SQL database.

Try something like this instead:

function TDBClass.addNome(nome: String): String;
var
  rsnome: TADOQuery;
begin
  rsnome := TADOQuery.Create(nil);
  try
    rsnome.Connection := connection;
    rsnome.SQL.Clear;
    rsnome.SQL.Text:='UPDATE enroll SET nome = :nome where id ="1"';
    rsnome.Parameters.ParamByName('nome').Value:= nome;
    rsnome.ExecSQL;
    rsnome.Close();
  finally
    rsnome.Free();
  end;
end;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top