procedure TFMain.btCreaFdbClick(Sender: TObject);
var
i,j: integer;
CampoPaso,NombreCampo,CamposSQL,ValoresSQL: String;
begin
if cbxTablas.Checked then
begin
IBDataBase1.Close;
IBDatabase1.DatabaseName := edDir.Text;
if FileExists(edDir.Text) then
begin
Mostrar('Ha ocurrido un problema al crear la base de datos.','Esta base de datos ya existe.');
Abort;
end;
IBDatabase1.Params.Clear;
IBDatabase1.Params.Add('USER '+QuotedStr(edUser.Text)+'');
IBDatabase1.Params.Add('PASSWORD '+QuotedStr(edPass.Text)+'');
IBDatabase1.Params.Add('PAGE_SIZE '+edPage.Text+'');
IBDatabase1.Params.Add('DEFAULT CHARACTER SET '+edChar.Text+'');
IBScript1.Database.DatabaseName := edDir.Text;
IBScript1.Script.Clear;
IBScript1.Script.AddStrings(Memo1.Lines);
try
IBDatabase1.CreateDatabase;
IBScript1.ExecuteScript;
except
on E: Exception do
begin
if E.ClassNameIs('EIBInterbaseError') then
begin
Mostrar('Se ha producido un error al crear la base de datos.', E.Message);
Abort;
end;
if e.ClassNameIs('EDataBaseError') then
begin
Mostrar('Se ha producido un error al crear la base de datos.', E.Message);
Abort;
end;
if e.ClassNameIs('EIBClientError') then
begin
Mostrar('Se ha producido un error al crear la base de datos.', E.Message);
Abort;
end;
end;
end;
end;
if cbxDatos.Checked then
begin
IBDatabase1.DatabaseName := edDir.Text;
for i := 0 to Tablas.Items.Count - 1 do begin
ZTablas1.Close;
cdTablas1.Close;
CampoBlob := False;
ZTablas1.TableName := Tablas.Items[i].Text;
Try
ZTablas1.Open;
cdTablas1.Open;
except
on E: Exception do
begin
if E.ClassNameIs('EZSQLException') then
begin
Mostrar('Se ha producido un problema al intentar abrir una de las tablas.',E.Message);
Abort;
end;
if E.ClassNameIs('EDataBaseError') then
begin
Mostrar('Se ha producido un problema al intentar abrir una de las tablas.',E.Message);
Abort;
end;
end;
End;
cdTablas1.First;
while not cdTablas1.Eof do begin
CamposSQL := '';
ValoresSQL := '';
for j := 0 to cdTablas1.FieldCount-1 do begin
NombreCampo := cdTablas1.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';
If UpperCase(NombreCampo) = 'NO' Then
NombreCampo := 'IDN';
If UpperCase(NombreCampo) = 'USER' Then
NombreCampo := 'USER1';
If UpperCase(NombreCampo) = 'DATE' Then
NombreCampo := 'DATE1';
CamposSQL := CamposSQL + NombreCampo;
case cdTablas1.Fields[j].DataType of
ftString,
ftWideString: begin
CampoPaso := cdTablas1.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;
ftTime : begin
if TimeToStr(cdTablas1.Fields[j].AsDateTime) = '' then
ValoresSQL := ValoresSQL+' NULL'
else ValoresSQL := ValoresSQL+#39+FormatDateTime('hh:mm:ss', cdTablas1.Fields[j].AsDateTime)+#39;
end;
ftDate,
ftDateTime,
ftTimeStamp : begin
if DateToStr(cdTablas1.Fields[j].AsDateTime) = '' then
ValoresSQL := ValoresSQL+' NULL'
else ValoresSQL := ValoresSQL+#39+FormatDateTime('dd.mm.yyyy', cdTablas1.Fields[j].AsDateTime)+#39;
ValoresSQL := StringReplace(ValoresSQL,'-','.',[rfReplaceAll]);
end;
ftLargeint,
ftInteger,
ftSmallint : begin
if cdTablas1.Fields[j].AsString = '' then
ValoresSQL := ValoresSQL+'0'
else ValoresSQL := ValoresSQL+cdTablas1.Fields[j].AsString;
end;
ftFloat,
ftCurrency : begin
if cdTablas1.Fields[j].AsString = '' then
ValoresSQL := ValoresSQL+'0.00'
else ValoresSQL := ValoresSQL+cdTablas1.Fields[j].AsString;
end;
ftBoolean : begin
if cdTablas1.Fields[j].Value = True then
ValoresSQL := ValoresSQL+'1'
else ValoresSQL := ValoresSQL+'0';
end;
else begin
ValoresSQL := ValoresSQL+cdTablas1.Fields[j].AsString;
end;
end;
if j < cdTablas1.FieldCount-1 then begin
CamposSQL := CamposSQL + ',';
ValoresSQL := ValoresSQL + ',';
end;
end;
IBQuery1.SQL.Text := 'INSERT INTO '+ Tablas.Items[i].Text+
' ('+CamposSQL+')'+
' VALUES ('+ValoresSQL+')';
// Si se desea se puede generar un log de las inserciones a la
// base de datos. Solo habilite las siguientes lineas.
//AssignFile(Arch,'Query.log');
//If FileExists('Query.log') then
// Append(Arch)
//else Rewrite(Arch);
//Writeln(Arch,IBQuery1.SQL.Text);
//CloseFile(Arch);
try
IBQuery1.ExecSQL;
except
on E: Exception do
begin
if E.ClassNameIs('EIBInterBaseError') then
begin
Mostrar('Ha ocurrido un problema en la migración de datos.', E.Message);
Abort;
end;
if E.ClassNameIs('EIBClientError') then
begin
Mostrar('Ha ocurrido un problema en la migración de datos.', E.Message);
Abort;
end;
end; //E: Exception
end; //Except
cdTablas1.Next;
end;
cdTablas1.Close;
ZTablas1.Close;
end;
end;
ShowMessage('Proceso de creación Finalizado');
end;