Ver Mensaje Individual
  #27  
Antiguo 26-04-2007
[basti] basti is offline
Miembro Premium
 
Registrado: ago 2004
Posts: 388
Reputación: 20
basti Va por buen camino
Código Delphi [-]
      while not Table1.Eof do begin
         CamposSQL  := '';
         ValoresSQL := '';
         for j := 0 to Table1.FieldCount-1 do begin
             NombreCampo := Table1.Fields[j].FieldName;
             NombreCampo := StringReplace(NombreCampo,'(','',[rfReplaceAll]);
             NombreCampo := StringReplace(NombreCampo,')','',[rfReplaceAll]);
             NombreCampo := TranslateChars(NombreCampo, CHARS_ACENTUADOS, CHARS_SINACENTO);
             If UpperCase(NombreCampo) = 'PASSWORD' Then
                NombreCampo := 'PASSCODE';
             If UpperCase(NombreCampo) = 'SIZE' Then
                NombreCampo := 'SIZE1';
             CamposSQL := CamposSQL + NombreCampo;
             case Table1.Fields[j].DataType of
                  ftString,
                  ftWideString: begin
                        CampoPaso  := Table1.Fields[j].AsString;
                        CampoPaso  := StringReplace(CampoPaso,'''','´',[rfReplaceAll]);
                        ValoresSQL := ValoresSQL+#39+CampoPaso+#39;
                  end;
                  ftMemo,ftBlob,
                  ftTypedBinary,
                  ftGraphic   : begin
                        ValoresSQL := ValoresSQL+' NULL';
                        //ValoresSQL := ValoresSQL+' :BLOB'+inttostr(i);
                        CampoBlob  := True;
                  end;
                  ftDate,
                  ftTime,
                  ftDateTime,
                  ftTimeStamp : begin
                        if Table1.Fields[j].AsString = '' then
                           ValoresSQL := ValoresSQL+' NULL'
                        else ValoresSQL := ValoresSQL+#39+Table1.Fields[j].AsString+#39;
                        ValoresSQL := StringReplace(ValoresSQL,'/','.',[rfReplaceAll]);
                  end;
                  ftInteger,
                  ftSmallint    : begin
                         if Table1.Fields[j].AsString = '' then
                            ValoresSQL := ValoresSQL+'0'
                         else ValoresSQL := ValoresSQL+Table1.Fields[j].AsString;
                  end;
                  ftFloat,
                  ftCurrency  : begin
                         if Table1.Fields[j].AsString = '' then
                            ValoresSQL := ValoresSQL+'0.00'
                         else ValoresSQL := ValoresSQL+Table1.Fields[j].AsString;
                  end;
                  ftBoolean   : begin
                         if Table1.Fields[j].Value = True then
                            ValoresSQL := ValoresSQL+'1'
                         else ValoresSQL := ValoresSQL+'0';
                  end;
                  else begin
                         ValoresSQL := ValoresSQL+Table1.Fields[j].AsString;
                  end;
             end;
             if j < Table1.FieldCount-1 then begin
                CamposSQL  := CamposSQL + ',';
                ValoresSQL := ValoresSQL + ',';
             end;
         end;
         IBQuery1.SQL.Text := 'INSERT INTO '+ ListBox1.Items[i]+
                              ' ('+CamposSQL+')'+
                              ' VALUES ('+ValoresSQL+')';


Creo que tendrás que usar parámetros. Yo lo haría así

Código Delphi [-]
  // primero creamos la consulta con los parámetros
  CamposSQL := ' (';
  ValoresSQL := ' (';
  for i := 0 to Table1.Fields.Count - 1 do
  begin
    CamposSQL := CamposSQL + Table1.Fields[i].FieldName + ',';
    ValoresSQL := ValoresSQL + ':' + Table1.Fields[i].FieldName + ',';
  end;
  // quitamos la coma sobrante al final
  Delete(CamposSQL, Length(CamposSQL), 1);
  Delete(ValoresSQL, Length(ValoresSQL), 1);
  CamposSQL := CamposSQL + ') ';
  ValoresSQL := ValoresSQL + ') ';

  with IBQuery1.SQL do
  begin
    Clear;
    Add('INSERT INTO ' + ListBox1.Items[i]);
    Add(CamposSQL);
    Add(' VALUES ' + ValoresSQL);
  end;

  // aquí traspasamos los datos usando los parámetros
  while not Table1.eof do
  begin
    for i := 0 to Table1.Fields.Count - 1 do
    begin
      // No sé si aceptará todos los tipos de datos, en todo caso habría que hacer
      // un case con los posibles tipos de datos
      IbQuery1.Params[i].DataType := Table1.Params[i].DataType;
      IbQuery1.Params[i].Value := Table1.Params[i].Value;
    end;
    IbQuery1.ExecSQL;
    Table1.Next;
  end;
Responder Con Cita