Delphi - Creating A Database Application Using Delphi
Delphi - Creating A Database Application Using Delphi
Database Application
using Delphi
Borland®
Kylix 3 ™
iii
iv
Creating a database application
Chapter0
Database application
Data module
UI
Data source Client dataset
Connection Unidirectional
Provider
component dataset
Database server
2 Tutorial
Setting up data access components
You use the Connection editor to select a connection configuration for the
TSQLConnection component or edit the connections stored in the dbxconnections
file in the .borland directory.
In the Connection editor, specify the pathname of the database file called
employee.gdb on your system. In this tutorial you will connect to a sample
InterBase database, employee.gdb, that is provided with Kylix. By default, the
InterBase installation places employee.gdb in /opt/interbase/examples.
6 Check the User_Name and Password fields for acceptable values. If you have not
altered the default values, you do not need to change the fields. If database access
is administered by someone else, you may need to get a username and password
to access the database.
7 When you are done checking and editing the fields, click OK to close the
Connection editor and save your changes.
These changes are written to the dbxconnections file and the selected connection is
assigned as the value of the SQLConnection component’s ConnectionName property.
8 Choose File|Save All to save your project.
4 Tutorial
Setting up data access components
3 Set the CommandText property to Select * from sales to specify the command that
the dataset executes. You can either type the Select statement in the Object
Inspector or click the ellipsis to the right of CommandText to display the
CommandText editor, where you can build your own query statement.
4 Set Active to True to open the dataset.
5 Choose File|Save All to save the project.
6 Tutorial
Designing the user interface
The DBGrid control displays data at design time, while you are working in the
IDE. This allows you to verify that you’ve connected to the database correctly. You
cannot, however, edit the data at design time; to edit the data in the table, you’ll
have to run the application.
5 From the Data Controls page, drop a TDBNavigator control onto the form. A
database navigator is a tool for moving through the data in a dataset (using next
and previous arrows, for example) and performing operations on the data.
6 Set the navigator bar’s DataSource property to DataSource1 so the navigator is
looking at the data in the client dataset.
7 Set the navigator bar’s ShowHint property to True. (Setting ShowHint to True allows
Help hints to appear when the cursor is positioned over each of the items on the
navigator bar at runtime.)
8 Choose File|Save All to save the project.
9 Press F9 to compile and run the project. You can also run the project by clicking the
Run button on the Debug toolbar, or by choosing Run from the Run menu.
When you run your project, the program opens in a window like the one you
designed on the form. You can test the navigation bar with the employee database.
For example, you can move from record to record using the arrow commands, add
records using the + command, and delete records using the – command.
Tip If you should encounter an error while testing an early version of your
application, choose Run|Program Reset to return to the design-time view.
Right-click in the
editor and choose
New Standard
Action to display
the Standard
Actions list box.
Select the actions
you want and click
OK. Press Ctrl to
select multiple
actions.
6 Select the following actions: TEditCopy, TEditCut, and TEditPaste. (Use the Ctrl key
to select multiple items.) Then click OK.
These standard actions appear in the Action List editor with default images
already associated with them.
8 Tutorial
Designing the user interface
7 Right-click the Action List editor and choose New Action to add another action
(not provided by default). Action1 is added by default. In the Object Inspector, set
its Caption property to Update Now!
This same action will be used on a menu and a button. Later on, you’ll add an
event handler so it will update the database.
8 Click (No Category), right-click and choose New Action to add another action.
Action2 is added. Set its Caption property to E&xit.
9 Click the X (in the upper right corner) to close the Action List editor.
You’ve added three standard actions plus two other actions that you’ll connect to
event handlers later.
10 Choose File|Save All to save the project.
Adding a menu
In this section, you’ll add a main menu bar with two drop-down menus—File and
Edit—and you’ll add menu items to each one using the actions in the action list.
1 From the Standard page of the Component palette, drop a TMainMenu component
onto the form. Drag it next to the other nonvisual components.
2 Set the main menu’s Images property to ImageList1 to associate the image list with
the menu items.
3 Double-click the TMainMenu component to display the Menu Designer.
4 Type &File to set the Caption property of the first top-level menu item and press
Enter.
When you type &File
and press Enter, the
top-level File command
appears ready for you
to add the first menu
item.
The ampersand before
a character activates an
accelerator key.
5 Select the blank menu item below the File menu. Set the blank menu item’s Action
property to Action2. An Exit menu item appears under the File menu.
6 Click the second top-level menu item (to the right of File). Set its Caption property
to &Edit and press Enter. Select the blank menu item that appears under the Edit
menu.
7 In the Object Inspector, set the Action property to EditCut1 and press Enter. The
item’s caption is automatically set to Cut and a default cut bitmap appears on the
menu.
8 Select the next blank menu item (under Cut) and set its Action property to EditCopy1
(a default copy bitmap appears on the menu).
9 Select the next blank menu item (under Copy) and set its Action property to
EditPaste1 (a default paste bitmap appears on the menu).
10 Select the next blank menu item (under Paste) and set its Caption property to a
hyphen (-) to create a divider line in the menu. Press Enter.
11 Select the next blank menu item (under the divider line) and set its Action property
to Action1. The menu item displays Update Now!
12 Click the X to close the Menu Designer.
13 Choose File|Save All to save the project.
10 Tutorial
Designing the user interface
14 Press F9 or Run on the toolbar to run your program and see how it looks.
Many of the commands on the Edit menu and the navigation bar are operational at
this time. Copy and Cut are grayed on the Edit menu until you select some text in the
database. You can use the navigation bar to move from record to record in the
database, insert a record, or delete a record. The Update command does not work yet.
Close the application when you’re ready to continue.
Adding a button
This section describes how to add an Update Now button to the application. This
button is used to apply any edits that a user makes to the database, such as editing
records, adding new records, or deleting records.
To add a button:
1 From the Standard page of the Component palette, drop a TButton onto the form.
(Select the component then click the form next to the navigation bar.)
2 Set the button’s Action property to Action1.
The button’s caption changes to Update Now! When you run the application, it
will be grayed out until an event handler is added to make it work.
12 Tutorial
Writing an event handler
9 Size the default image area to the size of the picture. Place the image next to the
label.
You can set the size of
the Image1 component
to match the size of the
picture inside it in two
ways: drag the edge of
Image1, or change the
Width and Height
properties in the Object
Inspector.
10 To align the text and the image, select both objects on the form, right-click, and
choose Align. In the Alignment dialog box, under Vertical, click Bottoms.
11 Choose File|Save All to save the project.
12 Press F9 to compile and run your application.
13 Close the application when you’re ready to continue.
3 Double-click Action1. In the Code editor, the following skeleton event handler
appears:
procedure TForm1.Action1Execute(Sender: TObject);
begin
end;
Right where the cursor is positioned (between begin and end), type:
if ClientDataSet1.State in [dsEdit, dsInsert] then ClientDataSet1.Post;
ClientDataSet1.ApplyUpdates(-1);
This event handler first checks to see what state the database is in. When you move
off a changed record, it is automatically posted. But if you don’t move off a changed
record, the database remains in edit or insert mode. The if statement posts any data
that may have been changed but was not passed to the client dataset. The next
statement applies updates held in the client dataset to the database.
Note Changes to the data are not automatically posted to the database when using
dbExpress. You need to call the ApplyUpdates method to write all updated, inserted,
and deleted records from the client dataset to the database.
end;
Right where the cursor is positioned (between begin and end), type:
Close;
This event handler will close the application when the File|Exit command on the
menu is used.
4 Close the Action List editor.
5 Choose File|Save All to save the project.
14 Tutorial
Writing an event handler
end;
Right where the cursor is positioned (between begin and end), type:
Action := caFree;
if ClientDataSet1.State in [dsEdit, dsInsert] then
ClientDataSet1.Post;
if ClientDataSet1.ChangeCount> 0 then
begin
Option := Application.MessageBox('You have pending updates. Do you want to write
them to the database?', 'Pending Updates',[smbYes, smbNo, smbCancel],
smsWarning, smbYes);
case Option of
smbYes: ClientDataSet1.ApplyUpdates(-1);
smbCancel: Action := caNone;
end;
end;
This event handler checks the state of the database. If changes are pending, they
are posted to the client dataset where the change count is increased. Then before
closing the application, a message box is displayed that asks how to handle the
changes. The reply options are Yes, No, or Cancel. Replying Yes applies updates to
the database; No closes the application without changing the database; and Cancel
cancels the exit but does not cancel the changes to the database and leaves the
application still running.
4 You need to declare the variable used within the procedure. On a line between
procedure and begin type:
var
Option: TMessageButton;
16 Tutorial
Index
A D
ActionList component 8 database applications
adding accessing 3 to 4
a database connection to an application 3 overview 1
a grid to an application 6 database sample, employee.gdb 4
a menu to an application 8 to 11 database user name and password 4
a title to an application 12 DataSetProvider component 5
an image to an application 12 datasets
an Update Now button to an application 11 client 5
unidirectional dataset component 4 unidirectional 4
applying edits to database 11 DataSource component 5
dbExpress 1
B DBGrid component 6
DBNavigator component 7
Button component 11
button, adding Update Now 11 E
C employee.gdb sample database 4
event handlers, creating 13 to 16
ClientDataSet component 5 events 13
code, writing 13 to 16
compiling programs 7 G
components
ActionList 8 grid, adding to an application 6
Button 11
ClientDataSet 5 I
DataSetProvider 5
DataSource 5 ibmgr utility 3
DBGrid 6 icons, adding to application 12
DBNavigator 7 Image component 12
Image 12 image, adding to application 12
ImageList 8 ImageList component 8
Label 12 Interbase server, starting 3
MainMenu 9
nonvisual 3 L
SQLConnection 3
Label component 12
SQLDataSet 3
connection component, adding 3
creating a project 2 M
MainMenu component 9
menu, adding to an application 8 to 11
Index v
N S
navigating data in the dataset 7 sample database 4
nonvisual components 3 SQLConnection component 3
SQLDataSet component 3
P
password for database 4
T
project title, adding to application 12
creating 2
running 7 U
R unidirectional dataset 4
updating a database 11
running applications 7 user name for database 4
vi Tutorial