Databases in Delphi Summary
Databases in Delphi Summary
1
Step 4: Setup ADOTable
• Change the following properties of the ADOTable (via the Object Inspector)
o Change the Name (tblXXXXX)
o Change the Connection to the ADOConnection you set up in Step 3
(conXXXXXX)
o Change the TableName to one of the databases table’s name
o Change the Active property to True
Step 7: Add Data Controls like a DBGrid (from Data Controls menu) and give them
suitable names (dbgXXXXX)
2
Using a Data Module
All interaction with the database is done with the ADOTable. If the database connection is
done directly in the form then you simply need to use the ADOTable name:
Example: tblCD.Active := True ;
If the ADOTable has been created on a data module, then you access the ADOTable via
the data module:
Example: dmDatabase.tblCD.Active := True ;
If you are referring to the ADOTable with multiple lines of code, it is easier to use the with
statement:
Example:
with dmDatabase do
begin
tblCD.Active := True ;
tblCD. ……
end;
3
Traversing the table
• Used when you need to go through each record in a table in order to find a record,
or calculate an average of a filed, etc.
Last
Example: tblCD.Last ; Moves pointer to last record in table.
Prior
Example: tblCD.Prior ; Moves pointer to previous record in table.
RecordCount
Example: iNum := tblCD.RecordCount ; Returns the number of records in a table.
• To obtain the value of a particular field, based on where the pointer is at:
o ADOTable, followed by square brackets with name of the field (as it appears
in the database) in single quotes inside the square brackets.
<ADOTable> [ ‘Field_Name’ ]
o Data type of the field in the database will be the data type that is returned:
▪ Text fields will return a string
▪ Number fields will return an integer or real depending on format
▪ Currency fields will return a real
▪ Date/Time field will return a TDateTime
▪ Yes/No field will return a Boolean
Example: iNum := tblPeople[ ‘Age’ ] ;
showmessage( tblCD[ ‘Artist’ ] + #9 + FloatToStr( tblCD[ ‘Price’ ] ) ) ;
4
• Examples of traversing a table in order to calculate an average of a field:
Sample Code – Calculating an Average
iCount := 0 ;
iSum := 0 ;
tblPeople.First ; //go to first record in table
while NOT tblPeople.EOF do //loop until end of the table
begin
if tblPeople[ ‘Group’ ] = ‘Admin’ then //check if record is Admin person
begin
iSum := iSum + tblPeople[ ‘Points’ ] ; //sum the points
inc( iCount ) ; //count the number of admin people
end; //end of if
tblPeople.Next ; //move to next record
end; //end of while
rAve := iSum / iCount ; //calculate average
showmessage( ‘Average points of admin group is ‘ + FloatToStr( rAve) ) ; // display Average
Code finds the average points (integer field) of only the records in the database in the Admin group (string
field). If you wanted to find average points of ALL the records in table then you can remove the If statement
as well as using the iCount variable. Average calculation can be done using:
rAve := iSum / tblPeople.RecordCount ;.
You need an iCount variable if counting specific records as in the example shown above.
Code displays the surnames (string field) of only the records in the database in the Admin group (string
field).
5
• Examples of traversing a table in order to find ONE specific record:
Sample Code – Searching for ONE record
if bFound = False then //if Boolean still false at this point, then record was never found
begin
<code> //Code that must execute if record is NOT found.
showmessage( ‘Record not found’ ) ;
end ; //end of if
Yellow highlighted code indicates changes to scenario when compared to searching for multiple records.
6
Creating a unique primary key
• You can use the searching for ONE record algorithm in order to determine if the
primary key given is not already in the table.
• If the primary key field is a number field then the following can be used to create a
new unique value:
Example:
tblPeople.Sort := ‘PersonID ASC’ ; //Sort on primary key
tblPeople.Last ; //go to last record, which will be largest value
iNewID := tblPeople[ ‘PersonID’ ] + 1 ; //get last primary key & + 1 to it
7
Sample Code – Editing multiple records
Code increases the points by 10 of all the records of people in the Admin group.
Deleting a record
• Make sure pointer is at the record that you want to remove
• Call the delete method: <ADOTable> .Delete ;
Example:
tblPeople.First ;
tblPeople.Delete ; //delete the first record
In other words,
First – pointer at record 1
Delete – record 1 removed and pointer moves to record 2
Next – pointer moves to record 3 (hence we have skipped record 2).
8
Sample Code – Deleting multiple records
Code deletes all records of those in the Admin group. NOTE: Else move to next.
If you do not use the Else in this case, if there are TWO admin records straight after each other, the second
one will be skipped.
9
EXTRA – Connecting to database using code
Step 1: Data Module <same as before>
• Create a new Data Module (File->New->Other->Delphi Files)
• Give the Unit file a name at the top (dmXXXXXXXX_U)
• Save as that same name (File->Save As-> use name from above)
Step 8: Add Data Controls like a DBGrid (from Data Controls menu) and give them
suitable names (dbgXXXXXX)
You should see the table’s fields and records in the DBGrid when the program is executed.