Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Trucos (https://www.clubdelphi.com/foros/forumdisplay.php?f=52)
-   -   Pasar parametros a string (https://www.clubdelphi.com/foros/showthread.php?t=86275)

tonioaburto 12-07-2014 00:50:36

Pasar parametros a string
 
Buenas tardes, cree los siguientes métodos, para pasar parámetros en cadenas como en otros lenguajes, si se puede mejorar estaría bien o si no espero les sirva como esta.
Código Delphi [-]
procedure Split(Delimiter: char; Str: string; ListOfStrings: TStrings) ;
begin
   ListOfStrings.Clear;
   ListOfStrings.Delimiter := Delimiter;
   ListOfStrings.DelimitedText := Str;
end;


function replaceParams(strBefore:string; values:string):string;
var
   contParam,i:integer;
   nStr:string;
   arStr:TStringList;
begin
    arStr := TStringList.Create;
    values:=StringReplace(values, ' ', '_',[rfReplaceAll, rfIgnoreCase]);
    Split(';', values, arStr) ;
    contParam:=0;
    nStr:='';
    for i:=1 to length(strBefore)  do begin
        if (strBefore[i] = '?') and (strBefore[i] <> ' ') then begin
            nStr := nStr + quotedStr(StringReplace(arStr[contParam], '_', ' ',[rfReplaceAll, rfIgnoreCase]));
            inc(contParam);
        end
        else begin
             nStr:= nStr + strBefore[i];
        end;
    end;
    Result := nStr;
end;


//se puede hacer una llamada como en el siguiente ejemplo
Procedure InsertReg(nombre:string; apellido:string; depto:integer);
var
   sentence, values:string;
begin
    values:=nombre+';'+apellido+';'+inttostr(depto);
    sentence := 'INSERT INTO empleados VALUES (?, ?, ?, 1)';
    accessData(replaceParams(sentence, values),'');
end;

ecfisa 12-07-2014 01:04:51

Hola tonioaburto, bienvenido a Club Delphi :)

Gracias por tu aporte ;)

Saludos :)


La franja horaria es GMT +2. Ahora son las 06:50:26.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi