Americo Torres
Americo Torres

Reputation: 23

Error Delphi / MySQL / Parameters

Well, the situation is: I have 1 TComboBox called cboTipDocIden and 1 TEdit called txtDocIdenSolic.

The values of cboTipDocIden are: "1.DNI","2.RUC".

The component TUniQuery is called q_DetSolicitante.

The SQL Sentence target is:

SELECT oper_solicitante.id_solicitante, oper_solicitante.ape_pat, oper_solicitante.ape_mat,
       oper_solicitante.nombre, oper_solicitante.direcc_idtipcalle, oper_solicitante.direcc_nombrecalle,
       oper_solicitante.direcc_nro, oper_solicitante.idvinculo_fk
FROM oper_solicitante
WHERE oper_solicitante.tipDocIden = :TipDocIden AND
      oper_solicitante.nroDocIden = :NroDocIden

The SQL is executed on OnExit event of txtDocIdenSolic, here the code:

procedure TForm1.txtDocIdenSolicExit(Sender: TObject);
var
  TipDocIden, NroDocIden:string;
begin
  if(length(txtDocIdenSolic.Text)>0) then
  begin
    TipDocIden:=chr(39)+copy(cboTipDocIden.Text,1,1)+chr(39);
    NroDocIden:=chr(39)+trim(txtDocIdenSolic.Text)+chr(39);
    q_DetSolicitante.Close;
    q_DetSolicitante.Params[0].AsString:=TipDocIden;
    q_DetSolicitante.Params[1].AsString:=NroDocIden;
    q_DetSolicitante.Open;

    if(length(q_DetSolicitante.FieldByName('id_solicitante').AsString)=0) then
    begin
      stbar.Panels[0].text:='Nuevo Solicitante...';
      txtApePat.SetFocus;
    end
    else
    begin
      stbar.Panels[0].Text:='Solicitante Reiterativo...';
      txtApePat.Text:=q_DetSolicitante.FieldByName('ape_pat').AsString;
      txtApeMat.Text:=q_DetSolicitante.FieldByName('ape_mat').AsString;
      txtNombre.Text:=q_DetSolicitante.FieldByName('nombre').AsString;
    end;
  end
  else
    msg1.Execute;
end;

Finally, the table structure is:

CREATE TABLE `oper_solicitante` (
  `id_solicitante` int(11) NOT NULL,
  `tipDocIden` char(1) NOT NULL,
  `nroDocIden` varchar(11) NOT NULL,
  `ape_pat` varchar(50) NOT NULL,
  `ape_mat` varchar(50) NOT NULL,
  `nombre` varchar(50) NOT NULL,
  `direcc_idtipcalle` int(11) NOT NULL,
  `direcc_nombrecalle` varchar(80) NOT NULL,
  `direcc_nro` varchar(15) NOT NULL,
  `idvinculo_fk` int(11) NOT NULL,
  PRIMARY KEY (`id_solicitante`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Well, the SQL returns zero rows inside Delphi but when i change the parameters for literal values the SQL works.

Thanks for your useful help.

Upvotes: 1

Views: 619

Answers (1)

Ken White
Ken White

Reputation: 125689

Remove the chr(39) characters around your parameter values. Using Params[].AsString allows the database driver to properly quote them, and you're adding (doubling) them and thus causing the query to fail.

TipDocIden:= copy(cboTipDocIden.Text,1,1);
NroDocIden:= trim(txtDocIdenSolic.Text);
q_DetSolicitante.Close;
q_DetSolicitante.Params[0].AsString := TipDocIden;
q_DetSolicitante.Params[1].AsString := NroDocIden;
q_DetSolicitante.Open;

Upvotes: 2

Related Questions