Lazarus
Programming => Databases => Topic started by: notebook on July 30, 2011, 09:18:38 pm
-
I try example from http://wiki.lazarus.freepascal.org/Lazarus_Database_Tutorial
but I get errors if I run app from lazarus on button press.
Project project1 raised exception class 'External'. SIGSEGV.
if I run app outside lazarus then I get message Access violation. problem is in
ds.DataSet:=query;
if I show data in memo then work
OS is fedora linux 14 32 bit, lazarus 0.9.30, ,31.03.2011
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
DBGrids, DbCtrls, Grids, sqldb, postgres, pqconnection,db;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
DBGrid1: TDBGrid;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
public
conn : TPQConnection;
query : TSQLQuery;
transaction : TSQLTransaction;
ds : TDatasource;
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
begin
conn := TPQConnection.Create(nil);
query := TSQLQuery.Create(nil);
transaction := TSQLTransaction.Create(nil);
try
try
conn.HostName := '127.0.0.1';
conn.UserName := 'postgres';
conn.Password := 'postgres';
conn.DatabaseName := 'postgres';
conn.Connected := True;
conn.Transaction := transaction;
query.DataBase := conn;
query.SQL.Text := 'select * from my_table';
query.Open;
ds.DataSet:=query;
DBGrid1.DataSource:=ds;
finally
query.Free;
conn.Free;
end;
except
on E: Exception do
ShowMessage(E.message);
end;
end;
end.
-
Although it is not possible to tell for sure without looking at your form, the place where you have the TDatasource defined suggests that you have not dropped a TDataSource on the form, but coded it yourself.
If so, then I do not see the TDatasource variable ds being created anywhere, which would explain the SIGSEGV.
Either code a create of "ds" or drop a TDataSource onto your form (or better, use a DataModule to separate view and model code).