HOME > >
TUTORIALS TABLE OF CONTENTS
- - - / - - / - - / - - / - - -
Other material for programmers
Delphi tutorial: Accessing database files. (Level Three)
There's a search button at the bottom of the page.
Please don't dismiss it because it isn't full of graphics, scripts, cookies, etc!
Click here if you want to know more about the source
and format of these pages.
Stop Press! Open Office Database
This tutorial was first written a long time ago. Since then, I have become a happy user of the excellent free database that is part of the free office suite "Open Office". Did I mention it is free? Don't dismiss it as being worth what you pay for it until you've tried it. You can even export things to formats accessible by those still in Microsofts' thrall.
Dt3e: Introduction to accessing files from databases
In this tutorial we take a look at accessing database files via Delphi. It is written using Delphi 2. I think the principles applied even in Delphi 1, but I'm not sure.
Oh heck! One sentence into this, and problems already! Where did "Cardfile" go? I was going to work with that as a common denominator we might all have. Oh well...
In this tutorial I'm talking about using data that you may have created, or obtained, which was originally (and may once again, one day) be accessed using one of the database managers such as dBase, Paradox, Access, SQL (actually a language, not a product, but if you don't understand, just ignore that entry in my list.)
This paragraph for search engines: This tutorial relates to db files, data arranged in records consisting of fields with data.
For the tutorial, we're going to interface a Delphi program we'll write to a database file originally created with Paradox. You may have to do things a *little* differently to adapt things for the database you use.
First catch your database...
The data will be pretty simple..... a single table with membership details for a club:
Member ID, Last name, First name, Member since
001, Adams, Jane, 1970
005, Brown, David, 1998
002, Jones, Jeremy, 1990
004, Smith, Simon, 2001
N.B.: There will be times when you are working with data and Delphi when you will have no need of any external database connectivity. THIS tutorial is about the times when you DO want to access data from non-Delphi-program-created database files. (See the "file handling" tutorials for the times your program is going to be the only one accessing the data in the files.) (You can, however, if you want to for some reason, write a whole RDMS package with Delphi... but why do that? Don't, however, think a Delphi program can't set up the table, etc, etc, if you program it to.)
Set up the database in whatever database manager you want to use. Paradox sometimes spawns index files, etc, etc, but for the simple case of this tutorial, just one file was needed: DD14CM.DB, (for "Club Membership") which I'll try to make available on my website. (It may not contain exactly the data shown above.)
BEFORE starting Delphi, because I was using Paradox, I also had to establish an alias for the datafile. Now, in a simple world, you have Paradox on the machine you want to use Paradox files on, and you can set up an alias within Paradox. On the system I'm writing theis tutorial on, I don't have Paradox, and I do have both the Borland Database Engine (BDE) and the more advanced IDAPI system, also from Borland, I believe. For BDE, the program for setting up the alias was BDECFG32. (Click the Pages menu item, then Alias). For IDAPI, the program was IDAPICFG.EXE. EVENTUALLY I got things right! Don't despair! The Delphi part of this is easy! (And the Paradox part would be easy, too, in normal circumstances. Lovely RDMS, Paradox, by the way. POWERFUL! FAST! (Not especially new-user friendly, I will admit.) Oh yes... Paradox owners can, legally, give clients what they need to work with tables created with Paradox. It is like the Adobe's policies regarding Acrobat.)
Once the external created database is available, put a copy someplace suitable. (Of course, in "real life" you'd have just the one copy, accessed both by your Delphi program(s) and the original database management software.) If you are using Paradox, you will also have to set up an alias (for that copy... the path to the data is part of the alias) I called my alias DD14CMA.
Click here to download database.
Click here to download sourcecode of Delphi program explained in this tutorial.
Having caught your database, begin the Delphi program....
Start Delphi, start a project called DD14. Main form: DD14f1, main unit DD14u1.
From the Data Access tab of the component pallette, add a TTable type object to the form. The default name (Table1) is fine. The TTable object is a non-visible component, like a TMainMenu object. Also from the Data Access tab, add a type TDataSource object to your form. It will also be non-visible. In the Object Inspector, click on DataSource1's DataSet property, and the property's combolist. You should find Table1 (the default name assigned to the TTable object you put on your form) available. Set DataSource1.DataSet to Table1.
From the Data Controls tab, add a DBGrid and DBNavigator to the form. Set DBGrid1.DataSource and DBNavigator.DataSource to DataSource1.
Set Table1's TableType. (I needed ttParadox, which was on the combobox list.) Also, set the DatabaseName. (I needed DD14CMA... Club Membership Alias) When you've done that, you should find your database file (DD14CM) available in the TableName combobox for Table1. (Set Table1.TableName to DD14CM). If all is well, you can now set the Active property to true. (If all is NOT well, you get an error message.) As soon as Active is set true, you should see some data from the database in the DBGrid... even before you run the application. (Make the DBGrid a suitable size.)
That's "the hard part"! Run the application. You should be able to edit the data in the database. You can add records with the plus sign on the DBNavigator (or the Insert key); you can remove records with the minus sign of the DBNavigator. Table1 has a ReadOnly property you can set to true if you want users to be restricted to reading the data, but prevented from changing it.
As with all Delphi learning, once you've got the basics working, you build up what you know by consulting the help file entries on the various properties, methods and events associated with the objects you are using.
The apparent complexity is there so Delphi can work easily with a wide range of possible external formats.
The TDataSource object creates the link (I am using the term diffrently from the use sometimes seen in relational database management) between the external database and the Delphi program. The Delphi components for database work have DataSource properties, which you set to the TDataSource object which you have linked to the data you want to work with. I believe this is a case of "encapsulation": the TDataSource object has a consistent interface with the Delphi database handling routines, regardless of the details of how the program, through the TDataSource object, actually accomplishes it's manipulation of the database files.
For example, the TTable object is "tied" to the data in the external file(s) through the TDataSource object.
This search merely looks for the words you enter. It won't answer "Where can I download InpOut32?"
The search engine is not intelligent. It merely seeks the words you specify. It will not do anything sensible with "What does the 'could not compile' error mean?" It will just return references to pages with "what", "does", "could", "not".... etc.
Also: This search only searches the material on one of my websites.
Click here to visit another of my sites.
Click here to visit my third site.
Click here if you're feeling kind! (Promotes my site via "Top100Borland")
Ad from page's editor: Yes.. I do enjoy compiling these things for you. I hope they are helpful. However... this doesn't pay my bills!!! Sheepdog Software (tm) is supposed to help do that, so if you found this stuff useful, (and you run a Windows or MS-DOS PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!
Click here to visit editor's freeware, shareware page.
Link to Tutorials main page
Here is how you can contact this page's author, Tom Boyd.
Page WILL BE tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org
If this page causes a script to run, why? Because of things like Google panels, and the code for the search button. Why do I mention scripts? Be sure you know all you need to about spyware.
....... P a g e . . . E n d s .....