Difference between revisions of "Working With TSQLQuery"

From Lazarus wiki
Line 1: Line 1:
 +
== General ==
 +
 
If you need to be able to DELETE or otherwise modify records, your database table should contain one PRIMARY KEY column that has the  property AUTO_INCREMENT. This enables the DELETE method to identify the records marked for deletion in the 'WHERE' clause written back to the database.
 
If you need to be able to DELETE or otherwise modify records, your database table should contain one PRIMARY KEY column that has the  property AUTO_INCREMENT. This enables the DELETE method to identify the records marked for deletion in the 'WHERE' clause written back to the database.
 
The primary key field should preferably be designed into the table structure at CREATE time, but can be added using the following example code in your MySQL client:
 
The primary key field should preferably be designed into the table structure at CREATE time, but can be added using the following example code in your MySQL client:
Line 9: Line 11:
  
 
Addition of a TDBNavigator toolbar makes it very easy to navigate through the records, and to select records for editing. When a record is selected by the toolbar or by moving the mouse through the data grid, the data for the relevant row and column appear in the TDBEdit box and if the 'Edit' button is clicked, the contents in the Edit box can be modified. Clicking on the 'Post' button confirms the change, or clicking on the 'Cancel' button cancels the changes.
 
Addition of a TDBNavigator toolbar makes it very easy to navigate through the records, and to select records for editing. When a record is selected by the toolbar or by moving the mouse through the data grid, the data for the relevant row and column appear in the TDBEdit box and if the 'Edit' button is clicked, the contents in the Edit box can be modified. Clicking on the 'Post' button confirms the change, or clicking on the 'Cancel' button cancels the changes.
 +
 +
== Cached Updates ==
 +
The ''TSQLQuery'' component caches all updates. That is, the updates are not sent immediately to the database, but are kept in memory till
 +
the ''APPLYUPDATES'' method is called. At that point, the updates will be transformed to SQL update statements, and will be applied to
 +
the database. If you do not call ''ApplyUpdates'', the database will not be updated with the local changes.
 +
 +
== Primary key fields ==
 +
When updating records, ''TSQLQuery'' needs to know which fields comprise the primary key that can be used to update the record.
 +
If your table has a primary key (as described above) then you only need to set the ''UsePrimaryKeyAsKey'' property to ''True'' and
 +
everything will be done for you.
 +
 +
If the table doesn't have a primary key index, but does have some fields that can be used to uniquely identify the record, then
 +
you can include the ''pfInKey'' option in the ''ProviderFlags'' property of these fields: When ''TSQLQuery'' needs to construct
 +
a WHERE clause for the update, it will collect all fields that have the ''pfInKey'' flag in their ''ProviderFlags'' property set,
 +
and will use the values to construct a WHERE clause which uniquely determines the record to update -- normally this is only needed
 +
for an update or delete statement.

Revision as of 13:56, 20 June 2008

General

If you need to be able to DELETE or otherwise modify records, your database table should contain one PRIMARY KEY column that has the property AUTO_INCREMENT. This enables the DELETE method to identify the records marked for deletion in the 'WHERE' clause written back to the database. The primary key field should preferably be designed into the table structure at CREATE time, but can be added using the following example code in your MySQL client:

 alter table testrig 
 add column autoid int 
 primary key auto_increment;

The code can, of course, be written on one line but has been broken up for clarity.

The data set returned by TSQlQuery can conveniently be viewed with an instance of TDBGrid, but it is not very suitable for editing the data in the individual fields and cells. For this purpose you need to place some Data-Aware single-field controls such as TDBEdit on your form, and set their DataSource poperty to the data source being used. The DataField property should be set to a named field (eg 'IDENTITY') or to some expression that returns a suitable string.

Addition of a TDBNavigator toolbar makes it very easy to navigate through the records, and to select records for editing. When a record is selected by the toolbar or by moving the mouse through the data grid, the data for the relevant row and column appear in the TDBEdit box and if the 'Edit' button is clicked, the contents in the Edit box can be modified. Clicking on the 'Post' button confirms the change, or clicking on the 'Cancel' button cancels the changes.

Cached Updates

The TSQLQuery component caches all updates. That is, the updates are not sent immediately to the database, but are kept in memory till the APPLYUPDATES method is called. At that point, the updates will be transformed to SQL update statements, and will be applied to the database. If you do not call ApplyUpdates, the database will not be updated with the local changes.

Primary key fields

When updating records, TSQLQuery needs to know which fields comprise the primary key that can be used to update the record. If your table has a primary key (as described above) then you only need to set the UsePrimaryKeyAsKey property to True and everything will be done for you.

If the table doesn't have a primary key index, but does have some fields that can be used to uniquely identify the record, then you can include the pfInKey option in the ProviderFlags property of these fields: When TSQLQuery needs to construct a WHERE clause for the update, it will collect all fields that have the pfInKey flag in their ProviderFlags property set, and will use the values to construct a WHERE clause which uniquely determines the record to update -- normally this is only needed for an update or delete statement.