Ver Mensaje Individual
  #1  
Antiguo 04-02-2016
andresjh87 andresjh87 is offline
Registrado
NULL
 
Registrado: ene 2016
Posts: 9
Reputación: 0
andresjh87 Va por buen camino
Exclamation Problema al insertar datos a DBF

Hola comunidad.

Tengo un problema que no le encuentro explicacion, uso delphi XE8 y agregue el componente TDBF.

El problema es que el insert o append no funciona. me dice delphi raised exception class EDATABASEerror cannot a modify read-only dataset. probe poniendo append, insert y nada, al llegar al insert pasa eso. me funciona perfecto el crear el dbf pero el insertar datos nada.

dejare el codigo por si alguien me puede ayudar


Código Delphi [-]
unit Unit6;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Data.DB, dbf, Vcl.Grids,
  Vcl.DBGrids, Vcl.ExtCtrls, Vcl.DBCtrls;

type
  TForm6 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Dbf1: TDbf;
    Dbf2: TDbf;
    Dbf3: TDbf;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Button3: TButton;
    DBNavigator1: TDBNavigator;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure DataSource1DataChange(Sender: TObject; Field: TField);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form6: TForm6;

implementation

{$R *.dfm}

procedure TForm6.Button1Click(Sender: TObject);
begin
Dbf2 := TDbf.Create(nil);
  try
    { make sure the data directory exists - otherwise this may fail (e.g. on Windows XP): }
    ForceDirectories('C:\EMPRESA\pruebaDBF\DATOS\');
    DeleteFile('C:\EMPRESA\pruebaDBF\DATOS\personas.dbf');
    DeleteFile('C:\EMPRESA\pruebaDBF\DATOS\personas.mdx');


    { use relative path to "data" directory }
    Dbf2.Close;
    Dbf2.FilePath := 'C:\EMPRESA\pruebaDBF\DATOS\';
    { we want to use Visual dBase VII compatible tables }
    Dbf2.TableLevel := 7;

    dbf2.ReadOnly:=false;
    //Dbf2.Exclusive := True;
    Dbf2.TableName := 'personas.dbf';
    With Dbf2.FieldDefs do begin
      Clear;
      Add('Rut', ftString, 15, false);
      Add('Nombre', ftString, 80, false);
    End;
    Dbf2.CreateTable;
    Dbf2.Open;
    Dbf2.AddIndex('personaId', 'Rut', [ixPrimary, ixUnique]);


    { add a secondary index }
    //Dbf1.AddIndex('custname','Name', [ixCaseInsensitive]);
    Dbf2.Close;
  finally
    Dbf2.Free;
  end;

end;

procedure TForm6.Button2Click(Sender: TObject);
var i:integer;
f1,f2:TField;
begin
//Dbf2:= TDbf.Create(self);
//with Dbf2 do
 //begin
 //Close;
 //Free;

 Dbf2.FilePath := 'C:\EMPRESA\pruebaDBF\DATOS\';
 Dbf2.TableLevel := 7;
 //dbf2.OpenMode:=omNormal;
 //Dbf2.Exclusive := true;
 Dbf2.TableName := 'personas.dbf';

 Dbf2.open;
 //Dbf2.First;
 //Dbf2.IndexName  :='personaId';

 //active:=true;

 try
 //Dbf2.Insert;
 Dbf2.Insert;
 Dbf2.FieldByName('Rut').AsString := '1-9';
 Dbf2.FieldByName('Nombre').AsString := 'Roxana';
 Dbf2.Post;

 except
       On E: Exception do begin
        Dbf2.Close;
        Dbf2.Free;
       end;
 end;
end;


procedure TForm6.Button3Click(Sender: TObject);
begin
Dbf3.FilePath := 'C:\EMPRESA\pruebaDBF\DATOS\';
Dbf3.TableName:='personas.dbf';
dbf3.Active:=false;

end;

procedure TForm6.DataSource1DataChange(Sender: TObject; Field: TField);
begin

end;

//end;

end.

Se los agradeceria demasiado (quiero salir de vacaciones u.u) jajajaa
Responder Con Cita