Difference between revisions of "Databases in Lazarus"

From Free Pascal wiki
m (Using Datasets)
(set up MW-redirect)
(Tag: New redirect)
(31 intermediate revisions by 9 users not shown)
Line 1: Line 1:
== Aim ==
#REDIRECT [[Databases]]
This page aims to provide a general overview of using databases in Lazaraus, and to provide a home from which other database pages  can be linked
Database use in Lazarus (or FreePascal) is fundamentally based on the TDataset class.  This represents a table or query to your application.  However, like many other such fundamental classes, you don't use the TDataset class it self, you use a descendant of it.  There are many of these.  They provide access to different kinds of databases, such as local dbase or text files, or back-end databases such as PostgreSQL, Firebird, MySQl and so forth.  Some dataset descendants link directly to database tables, while others use additional components or libraries to perform the link. 
The [[Databases]] page documents such descendants.
Dataset descendants, being non-visual components are (usually) part of the Free Component Library (FCL) rather than the Lazarus Component Library (LCL).
== Using Datasets ==
Datasets can be used both programmatically and with visual controls.  A typical Lazarus database application will often use both methods.  In either case, the first step is to create the TDataset descendant, initialise it to connect to the table or query you want, and open it.  This can be done either in code at run time or by putting a component on your form and setting it's properties at design time.  The details of this vary considerably with different TDataset descendants, so see the various guides under [[Databases]] for what has to be done for your database.
When the dataset is opened, a set of field components are created, one for each field or column of the table or query you opened.  Each field component is a descendant of TField, appropriate to the particular data type of the field, eg, TStringField.
===Using datasets from code===
Programmatic access will be explained in more detail in [[Using Dataset and Field components]], but as a very simple overview:
* Use the TDataset descendant to open the table or query, filter the rows you want to see, and to move from row to row. 
* Use the TField descendants to access general information about fields
* Also use the TField descendants to access the specific data values for the current row.  (use the As... properties, such as AsString, AsInteger, etc.)
* Access the fields of a TDataset descendant by using the fields property, eg Fields[0] is the first field, or by using the FieldByName method, eg FieldByName('AGE') returns the field associated with the database field called 'AGE'
===Using the visual data-aware controls===

Latest revision as of 02:00, 25 October 2019

Redirect to: