Lazarus: The Cross Platform Compiler (part II)

This article is my last experience about finding out how to make connection to database server within Lazarus application project. Based on my previous article, Lazarus natively support connection into several common database such as Oracle, PostgreSQL & MySQL.



The data components tab offered by Lazarus are so comprehensive & easy to use. My application scenario are planed to create an application over Linux box, connecting into MySQL 4.1.x server, querying a table which have only 5827 records & display it to the grid, also compared it with the same project re-compiled on a Window$ box to determine how long it takes time to display the result query.

At first, I put a button, a grid box, a DB navigator & a status bar components on a form. This button component required to opened the connection, the grid used to display rows, a DB navigator functioned to take a control the database manipulation operation, while the status bar will display the total records counted. There are some non-visual components also needed to link the connection into MySQL such as MySQL41Connection1, SQLTransaction1, SQLQuery1 & Datasource1.



While the complete example code I wrote below has the same structures as Delphi has (without the code completion which Lazarus not supported yet indeed).

unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Buttons,
DBGrids, ComCtrls, mysql41conn, DB, sqldb, DBCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Datasource1: TDatasource;
DBGrid1: TDBGrid;
DBNavigator1: TDBNavigator;
MySQL41Connection1: TMySQL41Connection;
SQLQuery1: TSQLQuery;
SQLTransaction1: TSQLTransaction;
StatusBar1: TStatusBar;
procedure Button1Click(Sender: TObject);
procedure StatusBar1DblClick(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
implementation

{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
begin
MySQL41Connection1.Connected:=false;
MySQL41Connection1.HostName:='server';
MySQL41Connection1.Databasename:='test_db';
MySQL41Connection1.UserName:='root';
MySQL41Connection1.Password:='password';
MySQL41Connection1.Connected:=true;
SQLQuery1.Database:=MySQL41Connection1;
DataSource1.DataSet:=SQLQuery1;
SQLQuery1.Close;
SQLQuery1.SQL.Clear;
SQLQuery1.SQL.Add('select count(*) as n from test_table');
SQLQuery1.Open;
StatusBar1.SimpleText:='Jumlah Record: '+SQLQuery1.fieldbyname('n').asstring;
SQLQuery1.Close;
SQLQuery1.SQL.Clear;
SQLQuery1.SQL.Add('select * from test_table');
SQLQuery1.Open;
end;

initialization
{$I unit1.lrs}
end.

Then I compiled & saved the projects named with browse. Lets see how much the ELF executable file size is? 8,78Mb! I re-compiled on Window$ and the EXE file size is more bigger, 10,8Mb!. No big deal, I back to the Linux and tried to run the application. Pressing the button to start the process until it shows rows in a grid would takes estimated 28 seconds. This relative same number happened while I tried to run over the Window$.



Note that on Window$, you have to provide the libmysql.dll referred to the same MySQL version to connecting to MySQL host, just the same you have to do if you open the connection with DBExpress component on Delphi. Just to compared, I also tried to made the same application over Delphi via ODBC connection. The time to processed above takes about 6 seconds!. Overall, this introduction to Lazarus makes me more steady to work on my next nearly incoming projects.

Sincerelly,

Eko Wahyudiharto
PS: If you've benefit from this blog,
you can support it by making a small contribution.

Enter your email address:

Delivered by FeedBurner

Post a Comment Bookmark and Share

 

Post a Comment

Leave comments here...