On 20/09/2011 14:53, Lukasz Sokol wrote:
> procedure TForm1.Button1Click(Sender: TObject);
> begin
>   if Label1.Caption = 'ODBC Disconnected' then
>     if OpenDialog1.Execute then
>       begin
> 
>         ODBCConnection1.Params.Add('DBQ='+OpenDialog1.FileName);
>        // ODBCConnection1.Open;
>         SQLQuery1.SQL.Clear;
>         SQLQuery1.SQL.Add('SELECT * FROM MyTable');
>         SQLQuery1.Open;  // SIGSEGV when calling a second time
>         exit;
>       end;
> ...snip....


Maybe we should stop you right there, and teach you the "correct" OOP
way of doing database programming. :) No GUI unit should contain code as
listed above... Database connections, Query components, embedded SQL,
manual transaction handling etc. Yuck!  Instead, use a OPF (Object
Persistent Framework) that hides all that ugliness and let you only work
with Business/Data Objects instead - the true OOP way.

For a broad overview of tiOPF (a free OPF framework for Delphi and Free
Pascal, and that works with VCL, LCL and fpGUI), see the following URL.

  http://tiopf.sourceforge.net/Doc/Concepts/index.shtml


For example, changing your code above to something as shown below:

var
  oList: TMyTableList;
begin
  oList := TMyTableList.Create;
  oList.Load;
  { now you have list of TMyTableItem objects to do as you please }


Now say we want to load that list, and display that list of objects in a
grid (named StringGrid1).

var
  oList: TMyTableList;
  FModelMediator: TtiModelMediator;
...

begin
  oList := TMyTableList.Create;
  oList.Load;

  FModelMeditor := TtiModelMediator.Create;
  FModelMediator.AddComposite('ID;Type;Name', StringGrid1);
  FModelMediator.Subject := oList;
  FModelMediator.Active := True;
end;

If you now Add or Remove or Change any data inside oList, the
StringGrid1 will automatically be updated for you.

NOTE:
The above code is done manually to show you the details of how it works.
You can also use the "RAD" style of development, by dropping a
TtiModelMediator component on a form and hooking up the properties via
the Object Inspector.


Now, saving the list back to the database is as simple as...

  oList.Save;

Transaction handling, rollback on error etc are all handled for you.

OPF frameworks like tiOPF really make using databases easy - including
creating UI for your data. tiOPF also works with multiple database
(Firebird, MySQL, Oracle, MDB, etc) and multiple database connection
components (SqlDB, FBLib, dbExpress, Zeos, UIB, OIB etc). So you are
definitely NOT locked into some specific vendor or suite of components.
And as you have seen from the example above, you application code
doesn't need to change at all, even if you switch from Firebird to
PostgreSQL, or from SqlDB to UIB components. It's all simply controlled
by a single compiler define.


   http://www.tiopf.com

See also the Lazarus wiki page:

   http://wiki.lazarus.freepascal.org/tiOPF


tiOPF has been used in production environments for well over a decade
and has just under 2000 unit tests which run hourly to make sure it
stays rock solid.


Regards,
  - Graeme -

-- 
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/


--
_______________________________________________
Lazarus mailing list
Lazarus@lists.lazarus.freepascal.org
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply via email to