Historia wymaga pasterzy, nie rzeźników.


The application must first perform an Edit to put the dataset in Edit state . To modify the current record, it must then perform a Post.
Since these methods depend explicitly on the structure of the underlying tables, an
application should use them only if the table structure will not change.
For example, the COUNTRY table has columns for Name, Capital, Continent, Area, and
Population. If Table1 were linked to the COUNTRY table, the following statement
would insert a record into the COUNTRY table:
Table1.InsertRecord(['Japan', 'Tokyo', 'Asia']);
The statement does not specify values for Area and Population, so it will insert Null
values for these columns. The table is indexed on Name, so the statement would insert
the record based on the alphabetic collation of “Japan”.
To update the record, an application could use the following code:
Table1.Edit;
Table1.SetRecord(nil, nil, nil, 344567, 164700000);
Table1.Post;
This code assumes that the cursor will be positioned on the record just entered for Japan.
It assigns values to the Area and Population fields and then posts them to the database.
Notice the three nils that act as place holders for the first three columns, which are not changed.
Setting the update mode
The UpdateMode property of a dataset determines how Delphi will find records being updated in a SQL database. This property is important in a multi-user environment
when users may retrieve the same records and make conflicting changes to them.
When a user posts an update, Delphi uses the original values in the record to find the
record in the database. This approach is similar to an optimistic locking scheme.
UpdateMode specifies which columns Delphi uses to find the record. In SQL terms, C h a p t e r 3 , U s i n g d a t a a c c e s s c o m p o n e n t s a n d t o o l s 65
UpdateMode specifies which columns are included in the WHERE clause of an UPDATE
statement. If Delphi cannot find a record with the original values in the columns
specified (if another user has changed the values in the database), Delphi will not make the update and will generate an exception.
The UpdateMode property may have the following values:
• WhereAll (the default): Delphi uses every column to find the record being updated.
This is the most restrictive mode.
• WhereKeyOnly: Delphi uses only the key columns to find the record being updated.
This is the least restrictive mode and should be used only if other users will not be
changing the records being updated.
• WhereChanged: Delphi uses key columns and columns that have changed to find the record being updated.
For example, consider a COUNTRY table with columns for NAME (the key), CAPITAL,
and CONTINENT. Suppose you and another user simultaneously retrieve a record with
the following values:
• NAME = “Philippines”
• CAPITAL = “Nairobi”
• CONTINENT = “Africa”
Both you and the other user notice that the information in this record is incorrect and
should be changed. Now, suppose the other user changes CONTINENT to “Asia,”
CAPITAL to “Manila,” and posts the change to the database. A few seconds later, you
change NAME to “Kenya” and post your change to the database.
If your application has UpdateMode set to WhereKey on the dataset, Delphi compares the original value of the key column (NAME = “Philippines”) to the current value in the
database. Since the other user did not change NAME, your update occurs. You think the
record is now [“Kenya,” “Nairobi,” “Africa”] and the other users thinks it is
[“Philippines,” “Asia," “Manila”]. Unfortunately, it is actually [“Kenya,” ,” “Asia,”
“Manila”], which is still incorrect, even though both you and the other user think you
have corrected the mistake. This problem occurred because you had UpdateMode set to its least restrictive level, which does not protect against such occurrences.
If your application had UpdateMode set to WhereAll, the Delphi would check all the columns when you attempt to make your update. Since the other user changed
CAPITAL and CONTINENT, Delphi would not let you make the update. When you
retrieved the record again, you would see the new values entered by the other user and
realize that the mistake had already been corrected.
Bookmarking data
It is often useful to mark a particular location in a table so that you can quickly return to it when desired. Delphi provides this functionality through bookmark methods. These methods enable you to put a bookmark in the dataset, and quickly return to it later.
66
D a t a b a s e A p p l i c a t i o n D e v e l o p e r ’ s G u i d e