fix american-centric dates
← Older revision
Revision as of 12:02, 28 March 2014
(3 intermediate revisions by one user not shown)
Line 1:
Line 1:
{{Adventures of a Newbie}}
{{Adventures of a Newbie}}
−
{{Note|This article was originally written years ago and a lot of things have changed/improved since then
. However, the article is kept because it gives a good description of what new users may encounter and also can be updated with links to new documentation etc
.}}
+
{{Warning|As indicated by the writer, this article was written by a newbie who may not have known the best way of doing things. The code and solutions presented may very well not be optimal. Please be aware of this and verify solutions with other documentation; please add links to articles with improved code/solutions. However, having the thought processes of a new user described could be quite valuable for others in the same situation}}
+
{{Note|This article was originally written years ago and a lot of things have changed/improved since then.}}
I'm writing this because like you I'm a newbie (as of today only five days under my belt). As a newbie I ran into many issues that I believe would have stopped others dead in their tracks. Not that I'm some sort of genius or better than others – just hard headed. To make matters worse I'm coming from the MS windows world and I'm learning Lazarus on a Linux system. Worse of all I have no Delphi experience.
I'm writing this because like you I'm a newbie (as of today only five days under my belt). As a newbie I ran into many issues that I believe would have stopped others dead in their tracks. Not that I'm some sort of genius or better than others – just hard headed. To make matters worse I'm coming from the MS windows world and I'm learning Lazarus on a Linux system. Worse of all I have no Delphi experience.
Line 9:
Line 10:
===Installation===
===Installation===
−
I am using SUSE 10.0.
That is important because Novell SUSE installs without the Gnome support development libraries installed.
In fact I think most of the the Linux distributions by default do not install the development libraries.
To make matters worse SUSE's default window manager is KDE.
Again, that matters because the KDE uses the QT graphic libraries. Now that I have said that I need to tell you that Lazarus uses the GTK graphic libraries in the default installation.
Although, as of today
03/
19
/06
there are a few developers that are trying to setup the QT (KDE uses the QT) libraries for Lazarus.
+
I am using SUSE 10.0. That is important because Novell SUSE installs without the Gnome support development libraries installed. In fact I think most of the the Linux distributions by default do not install the development libraries. To make matters worse SUSE's default window manager is KDE. Again, that matters because the KDE uses the QT graphic libraries. Now that I have said that I need to tell you that Lazarus uses the GTK graphic libraries in the default installation. Although, as of today 19
March 2006
there are a few developers that are trying to setup the QT (KDE uses the QT) libraries for Lazarus.
So before downloading the FPC (Free Pascal Compiler) and Lazarus IDE files you should install the development graphic libraries. In my case that was very easy. SUSE 10.0 has a utility called 'YAST'. Open YAST click on the software management icon. It opens a search window - type 'GTK' for the search string. The search screen returns a list of all the GTK items available. I checked everything because I really did not know what was required. YAST suggest other software packages to install. I just agreed to everything. I'm sure that someone out in the GTK world knows exactly what was needed but I sort of shotgun the process. It was simple.
So before downloading the FPC (Free Pascal Compiler) and Lazarus IDE files you should install the development graphic libraries. In my case that was very easy. SUSE 10.0 has a utility called 'YAST'. Open YAST click on the software management icon. It opens a search window - type 'GTK' for the search string. The search screen returns a list of all the GTK items available. I checked everything because I really did not know what was required. YAST suggest other software packages to install. I just agreed to everything. I'm sure that someone out in the GTK world knows exactly what was needed but I sort of shotgun the process. It was simple.
Line 45:
Line 46:
−
So how do Lazarus SQLdb controls work?
Normally I could find a tutorial that some kind soul provided on the web that would help me understand.
But as of
03/
24
/
06 I could not find a tutorial. (March 2014: [[Lazarus Database Overview]] is available but has since been changed from a tutorial to an overview page. See [[SQLdb Tutorial1]] for a newer tutorial series that should be very usable) In fact very little was written about the SQLdb controls on the web. I had the source code to SQLdb (it is in the Free Pascal Compiler source) but as a newbie to pascal I found reading the code almost impossible (although the comments were a little helpful).
I did have a few examples that were in the FPC source code folder.
I did not find these examples they had to be pointed out to me by the author of SQLdb via the Lazarus mail-list.
So from the examples is where I started.
I had only one advantage.
I know the postgres database engine from a past programing job.
Armed with the examples and the ability to setup a postgres log I charged in.
+
So how do Lazarus SQLdb controls work? Normally I could find a tutorial that some kind soul provided on the web that would help me understand. But as of 24
March
06 I could not find a tutorial. (March 2014: [[Lazarus Database Overview]] is available but has since been changed from a tutorial to an overview page. See [[SQLdb Tutorial1]] for a newer tutorial series that should be very usable) In fact very little was written about the SQLdb controls on the web. I had the source code to SQLdb (it is in the Free Pascal Compiler source) but as a newbie to pascal I found reading the code almost impossible (although the comments were a little helpful).
+
+
I did have a few examples that were in the FPC source code folder. I did not find these examples they had to be pointed out to me by the author of SQLdb via the Lazarus mail-list. So from the examples is where I started. I had only one advantage. I know the postgres database engine from a past programing job. Armed with the examples and the ability to setup a postgres log I charged in.
Before you can perform any operation on a database you must make a connection to it. Therefore the first thing is to click on the SQLdb tab on the Lazarus Editor toolbar. You will see several icons. For now we are only interested in the connection icon for Postgres. The icon with the elephant as the picture (elephants never forget – that why the elephant). Of course other database engines are supported – but like I said I only know what I have done to get postgres working. Let's continue, select and drag and drop a connection on to the form. Lazarus will name the control for you - so we will just use the name provided.
Before you can perform any operation on a database you must make a connection to it. Therefore the first thing is to click on the SQLdb tab on the Lazarus Editor toolbar. You will see several icons. For now we are only interested in the connection icon for Postgres. The icon with the elephant as the picture (elephants never forget – that why the elephant). Of course other database engines are supported – but like I said I only know what I have done to get postgres working. Let's continue, select and drag and drop a connection on to the form. Lazarus will name the control for you - so we will just use the name provided.
Line 141:
Line 144:
John Fabiani
John Fabiani
+
== SQLDB and connections ==
+
{{Note|See [[SQLdb Tutorial4]] for a solution to the problems mentioned below: using Data Modules which is the preferred way of managing a connection and a shared transaction for an entire application}}
I'm Back!
I'm Back!
Line 146:
Line 151:
Change my thinking or don't use SQLdb.
Change my thinking or don't use SQLdb.
−
In the windows world I have always used an ODBC connection to the database engine for my connections from FoxPro.
I have come to expect that I controlled when a transaction starts and stops.
But in the Delphi/FPC world this is not the way it works. The act of setting the transaction.active:=True; causes the transaction control to send a 'begin' to the database engine. This means if you are just requesting data from the database as in “Select * from sometable” it is done within a SQL transaction. In other words a 'begin' was sent to the database engine. BTW the corresponding “Rollback” or “Commit” is not sent automatically after retrieving the data. So what has to occur is as follows:
+
In the windows world I have always used an ODBC connection to the database engine for my connections from FoxPro. I have come to expect that I controlled when a transaction starts and stops.
+
But in the Lazarus/FPC world this is not the way it works. The act of setting the transaction.active:=True; causes the transaction control to send a 'begin' to the database engine. This means if you are just requesting data from the database as in “Select * from sometable” it is done within a SQL transaction. In other words a 'begin' was sent to the database engine. BTW the corresponding "Rollback" or "Commit" is not sent automatically after retrieving the data. So what has to occur is as follows:
+
+
<syntaxhighlight>
transaction.starttransaction; // the same as transaction.active:=true;
transaction.starttransaction; // the same as transaction.active:=true;
sqlquery.open; //this already has the sql statement “select * from sometable”
sqlquery.open; //this already has the sql statement “select * from sometable”
transaction.endtransaction; // the same as transaction.active:=False;
transaction.endtransaction; // the same as transaction.active:=False;
+
</syntaxhighlight>
+
+
This of course is very different than what I'm used to. I sort got my head around using the transaction start and end. But that is not all that is different. Again, in the windows world I only used one connection to the database for everything (well almost everything). So in my coding with FPC I attempted to use one connection (one pqconnection). Man this proved to be difficult. It worked but it was a lot of coding.
+
+
I asked the mailing list how everyone handled this question (use one connection or use multi connections) I got no response. I'm not sure what that means but I checked the web for delphi examples of data access with multiple forms.
+
+
In each example (remember these were for delphi) the authors used multiple connections. One for each form. So now I'm using a new connection for each form I open. This actually made the coding much easier. I still had to setup the connection fields (databasename, username,password, etc) in code but the rest of the data access controls can be set at design time and required no setup coding.
−
This of course is very different than what I'm used too. I sort got my head around using the transaction start and end. But that is not all that is different. Again, in the windows world I only used one connection to the database for everything (well almost everything). So in my coding with FPC I attempted to use one connection (one pqconnection). Man this proved to be difficult. It worked but it was a lot of coding. I asked the mailing list how everyone handled this question (use one connection or use multi connections) I got no response. I'm not sure what that means but I checked the web for delphi examples of data access with multiple forms. In each example (remember these were for delphi) the authors used multiple connections. One for each form. So now I'm using a new connection for each form I open. This actually made the coding much easier. I still had to setup the connection fields (databasename, username,password, etc) in code but the rest of the data access controls can be set at design time and required no setup coding.
But what this means is I will more than one connection to the database per each user of my program.
Image a user with 10 forms open and ten users. This means I could have 100 connections open to the database engine.
Is this important? I know that the Postgres database engine can support thousands of connections.
So in the end I guess not.
I just have to set a parameter in the Postgres configuration.
So I have changed my thinking.
</pre>
+
But what this means is I will more than one connection to the database per each user of my program. Image a user with 10 forms open and ten users. This means I could have 100 connections open to the database engine. Is this important? I know that the Postgres database engine can support thousands of connections. So in the end I guess not. I just have to set a parameter in the Postgres configuration. So I have changed my thinking.
==Add a Login Screen==
==Add a Login Screen==
−
04/
17
/06
Adding a login screen (splash screen too).
+
April
17
, 2006
Adding a login screen (splash screen too).
OK so I will need a login screen because my program accesses a database. It has to be the first screen the user will see but not the main screen. My main screen is a form with a bunch of buttons (sort of a menu of buttons) to call other forms and I want it to control and close all the other forms should the user close the main form. So how do I get a form to display before the main screen opens? Searching the internet provided part of the answer. Researching the Lazarus forum provided even more and including the the trick to making a splash screen to work.
OK so I will need a login screen because my program accesses a database. It has to be the first screen the user will see but not the main screen. My main screen is a form with a bunch of buttons (sort of a menu of buttons) to call other forms and I want it to control and close all the other forms should the user close the main form. So how do I get a form to display before the main screen opens? Searching the internet provided part of the answer. Researching the Lazarus forum provided even more and including the the trick to making a splash screen to work.