ilichhernandez
27-06-2006, 21:45:17
Holas a todos, bueno estoy tratando de leer un archivo de texto que tiene registros y estos a medida de irlos leyendo traspasarlos a una tabla de interbase. He intentado con varias variantes del siguiente código
procedure TForm1.dxButton1Click(Sender: TObject);
var
openDialog : TOpenDialog;
myFile : TextFile;
estado : variant;
text : string;
cadena,n1,n2 : String;
n3 : integer;
numinv : boolean;
vficha : integer;
vnombre : string;
vRut : string;
vsueldo : integer;
vdigvernum : integer;
vdigverstr : string;
begin
openDialog := TOpenDialog.Create(self);
opendialog.InitialDir := 'F:\junio_2006.txt';
openDialog.Options := [ofFileMustExist];
openDialog.Filter :=
'Archivos de texto|*.txt|Todos loa archivos|*.*';
openDialog.FilterIndex := 2;
if openDialog.Execute
then
begin
LMDEdit5.Text := opendialog.FileName;
end
else ShowMessage('La operacion de apertura de archivo fue cancelada !!');
openDialog.Free;
Assignfile(myFile, LMDEdit5.text);
Reset(myFile);
while not Eof(myFile) do
begin
vficha := 0;
vnombre :='';
vrut :='';
ReadLn(myFile, n1);
n2 := Trim(n1);
n3 := length(n2);
cadena := (Copy(n2, 1, 1));
If (isinteger(cadena)) then
begin
If (n2<>'')and((n3=41)or(n3=42)or(n3=43)or(n3=9)or(n3=10)or(n3=11)) then
begin
if (n3>10) then
begin
vficha:=strtoint(trim(copy(n2,1,2)));
delete(n2,1,3);
trimleft(n2);
n3 := length(n2);
vrut := (copy(n2,n3-10,11));
trim(vrut);
n2 := copy(n2,1,29);
vnombre := copy (n2,1,28);
vnombre := n2;
query1.close;
query1.sql.Clear;
query1.SQL.Add('Insert into tabla (ficha,nombre,rut,sueldo)');
query1.sql.add('values(:nficha,:nnombre,:nrut,:nsueldo)');
query1.ParamByName('nficha').AsInteger := vficha;
query1.ParamByName('nnombre').asString := vnombre;
query1.parambyName('nrut').AsString := vrut;
query1.ParamByName('nsueldo').AsInteger := vsueldo;
query1.ExecSQL;
//showmessage('ficha='+ inttostr(vficha) + ' nombre='+vnombre + ' rut=' + vrut);
vficha := 0;
vnombre:='';
vrut := '';
end;
end;
end;
end;
closefile(myFile);
end;
Como pueden ver, primero pido el archivo a leer, luego voy tomando los datos y los separo en las variables que luego quiero guardar en la tabla.
Las variables a guardar son ficha, nombre,rut y sueldo.
A Todos gracias de antemanos por sus consejos y ayudas.
procedure TForm1.dxButton1Click(Sender: TObject);
var
openDialog : TOpenDialog;
myFile : TextFile;
estado : variant;
text : string;
cadena,n1,n2 : String;
n3 : integer;
numinv : boolean;
vficha : integer;
vnombre : string;
vRut : string;
vsueldo : integer;
vdigvernum : integer;
vdigverstr : string;
begin
openDialog := TOpenDialog.Create(self);
opendialog.InitialDir := 'F:\junio_2006.txt';
openDialog.Options := [ofFileMustExist];
openDialog.Filter :=
'Archivos de texto|*.txt|Todos loa archivos|*.*';
openDialog.FilterIndex := 2;
if openDialog.Execute
then
begin
LMDEdit5.Text := opendialog.FileName;
end
else ShowMessage('La operacion de apertura de archivo fue cancelada !!');
openDialog.Free;
Assignfile(myFile, LMDEdit5.text);
Reset(myFile);
while not Eof(myFile) do
begin
vficha := 0;
vnombre :='';
vrut :='';
ReadLn(myFile, n1);
n2 := Trim(n1);
n3 := length(n2);
cadena := (Copy(n2, 1, 1));
If (isinteger(cadena)) then
begin
If (n2<>'')and((n3=41)or(n3=42)or(n3=43)or(n3=9)or(n3=10)or(n3=11)) then
begin
if (n3>10) then
begin
vficha:=strtoint(trim(copy(n2,1,2)));
delete(n2,1,3);
trimleft(n2);
n3 := length(n2);
vrut := (copy(n2,n3-10,11));
trim(vrut);
n2 := copy(n2,1,29);
vnombre := copy (n2,1,28);
vnombre := n2;
query1.close;
query1.sql.Clear;
query1.SQL.Add('Insert into tabla (ficha,nombre,rut,sueldo)');
query1.sql.add('values(:nficha,:nnombre,:nrut,:nsueldo)');
query1.ParamByName('nficha').AsInteger := vficha;
query1.ParamByName('nnombre').asString := vnombre;
query1.parambyName('nrut').AsString := vrut;
query1.ParamByName('nsueldo').AsInteger := vsueldo;
query1.ExecSQL;
//showmessage('ficha='+ inttostr(vficha) + ' nombre='+vnombre + ' rut=' + vrut);
vficha := 0;
vnombre:='';
vrut := '';
end;
end;
end;
end;
closefile(myFile);
end;
Como pueden ver, primero pido el archivo a leer, luego voy tomando los datos y los separo en las variables que luego quiero guardar en la tabla.
Las variables a guardar son ficha, nombre,rut y sueldo.
A Todos gracias de antemanos por sus consejos y ayudas.