Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2008-05

Access a sqlite3 database from within a class

3 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Paul van der Vlis

Hello list!

I would like to define a class, Tklasje, of my own which contains a database in the private section. Using this class I want to access the database. (another Lazarus-application could succesfully show this database, so I assume the database is OK).

The class is defined in unit2 and used in unit1 (see below). When running this little program I see the TEdit change to "gecreeerd" but then I get an error message: "acces violation". This doesn't change when I change the DoeIets procedure of the Tklasje class into something different like:

procedure Tklasje.DoeIets;
begin
  d_database.First;
end;

What do I do wrong? Can anyone help me find a solution?

With kind regards,

Paul van der Vlis.

----------------------------------------------
unit unit2;
{$mode objfpc}{$H+}
interface
uses
  Classes, SysUtils, sqlite3ds, DbCtrls, sqldb, db;
type
  Tklasje = class
  private
    d_database : TSqlite3Dataset;
  public
    constructor Create;
    destructor Destroy; override;
    procedure DoeIets;
end;
implementation
constructor Tklasje.Create;
begin
  d_database := TSqlite3Dataset.Create(nil);
  try
    with d_database do
      begin
        if Active then Close;
        FileName := 'sqlite3filenaam';
        TableName := 'tabelnaam';
        Open;
      end;
  except
    d_database.Free;
    d_database := nil;
  end;
end;
destructor Tklasje.Destroy;
begin
  d_database.Free;
  d_database := nil;
  Inherited;
end;
procedure Tklasje.DoeIets;
begin
  d_database.Active := true;
end;
initialization
end.
----------------------------------------------
unit unit1;
{$mode objfpc}{$H+}
interface
uses
  Classes, SysUtils, LResources, Forms, unit2, StdCtrls;
type
  { TSchermpje }
  TSchermpje = class(TForm)
    status: TEdit;
    procedure statusClick(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;
var
  MijnSchermpje: TSchermpje;
implementation
procedure TSchermpje.statusClick(Sender: TObject);
var
  MijnKlasje : TKlasje;
begin
  status.Text := 'creeren';
  MijnKlasje.Create;
  status.Text := 'gecreeerd';
  MijnKlasje.DoeIets;
  status.Text := 'gedaan';
  MijnKlasje.Destroy;
  status.Text := 'klaar';
end;
initialization
  {$I unit1.lrs}
end.

----------------------------------------------

Met vriendelijke groet,
Paul van der Vlis.

Reply by email

Flávio Etrusco
to Paul van der Vlis

. (...)

Show quoted text 27 lines
What do I do wrong? Can anyone help me find a solution? With kind regards, Paul van der Vlis. (...) procedure TSchermpje.statusClick(Sender: TObject); var MijnKlasje : TKlasje; begin status.Text := 'creeren'; >>> MijnKlasje.Create; status.Text := 'gecreeerd'; MijnKlasje.DoeIets; status.Text := 'gedaan'; MijnKlasje.Destroy; status.Text := 'klaar'; end; initialization {$I unit1.lrs} end.

You got the 'MijnKlasje' instantiation wrong - but I see you got the others right ;-)
MijnKlasje.Create; --> MijnKlasje := TKlasje.Create;

Once again, does anyone know any explanation for this seemingly stupid language design decision of Borland?
I hope the initialization/reset argument isn't true... Anyway, does FPC really need to conform (or could I say 'abide'?) to it?

-Fl?vio

Reply by email

Paul van der Vlis
to Flávio Etrusco
Fl?vio Etrusco wrote 32 lines
. (...) > What do I do wrong? Can anyone help me find a solution? > > With kind regards, > > Paul van der Vlis. > > > (...) > > procedure TSchermpje.statusClick(Sender: TObject); > var > MijnKlasje : TKlasje; > begin > status.Text := 'creeren'; >>>> MijnKlasje.Create; > status.Text := 'gecreeerd'; > MijnKlasje.DoeIets; > status.Text := 'gedaan'; > MijnKlasje.Destroy; > status.Text := 'klaar'; > end; > > initialization > {$I unit1.lrs} > > end. > You got the 'MijnKlasje' instantiation wrong - but I see you got the others right ;-) MijnKlasje.Create; --> MijnKlasje := TKlasje.Create;

You are right. Wow, it works!

With regards,
Paul.

Reply by email