Jump to content




Recent Status Updates

View All Updates

Developed by Kemal Taskin
Photo
- - - - -

Very Basic Database Application In Delphi

delphi

  • Please log in to reply
6 replies to this topic

#1 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,318 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 20 February 2011 - 01:34 PM

Creating database application in Delphi is very easy, since Delphi provides a very nice and powerful framework for manipulating database. The framework was based on VCL (Visual Component Library), making it possible to do most (if not all) of the database manipulation to be done in design time (visually). And this is what this tutorial tries to show you, i.e. build a simple database application with Delphi using very minimal codes.


Note that without writing more complex code, the application could not do much. So don't expect to be able to write a real world database application without writing codes. However the understanding of how to do things in design time (visually) will greatly reduce your coding time. :)


Basic Scheme

Let's start by explaining the basic scheme of database handling in Delphi. In Delphi's database framework, you will need 3 kinds of components to work with a database.


Dataset

The first kind of component you need is dataset component. Dataset component is components that provides means to operate on data. For example, you need dataset component to retrieve data from database, you also need dataset component to insert new data, update data, and delete data. The example of such components includes: (in BDE pallette) TTable, TQuery, (in ADO pallette) TADODataset, TADOQuery, and TADOTable.



Connection

The second kind component you need is component that provides connection to the database. This component allows to define a single point of connection that can be used by several dataset. With this component, you don't have to specify the database settings (such as db location, db name, and db login information) for each dataset you have. Examples of this component are: (in BDE pallette) TDatabase, and (in ADO pallette) TADOConnection.


Data Controls

The last but not the least component you need is data controls. These components are what you need to show data to users and also to capture inputs. They are what users have to use to work on their data. Examples of data controls: TDBEdit (to handle data in single line), TDBMemo (to handle multiple lines of data), TDBText (to show static data, behaves like TLabel), TDBImage, TDBListBox (to show list of options), and TDBGrid (to show multiple records in table-like control).


Our First Database Application

Let's start building our first database application. This one is a really simple one for its purpose is just to introduce you to the basic. So in this project we only build an application to show the content of a table.

For this project we will use a database created with MS Access. The sample database is attached with this tutorial, feel free to download it first (see link in the end of this tutorial). The name of the database is db.mdb. The database only contains a table with name Table1 with structure of:

  ID        : Integer, autoincrement;
  FirstName : String, max 40 chars;
  LastName  : String, max 40 chars;
  Note      : String, max 255 chars;


1. Create new project.

In Delphi IDE create new project by open menu File and select menu item New - Application. You will get new empty form. Save the application and the form. Let's give the application name HelloDb, and give the form name of FormMain and save it as Form_Main.pas. Don't forget to copy our database (db.mdb) to the same location of the project file.


2. Drop and configures TADOConnection

To connect with MS Access database we need TADOConnection. So click on ADO pallette, click on ADOConnection icon, and then click on the main form. Now we have a fresh TADOConnection in our main form. Name it MainConnection. Time to configure it to connect to our database.

To configure TADOConnection in design time, just double click it. A configuration dialog similar with the image below will show.

ADOConnectionSetting_Dlg001.jpg


For this project we want to provide database settings using connection string For it's easier to do. So click the Build button. You will be presented with a dialog with four tabs, like shown in the following image.

ADOConn_SelectMSJet4_001.jpg


In the Provider tab, make sure you select Microsoft Jet 4.0. OLE DB Provider. Like shown above. It's the database driver that our TADOConnection will use to connect with our database.

In the Connection tab, you have to supply the database name and location. Use the ellipsis button to browse for the database. After you have selected the correct db file you can test whether you have provided correct information or not by clicking on Test connection button. Ignore the other fields in this tab and also ignore the other tabs for they are not relevant for our current project.

ADOConnConnectionTab_001.jpg


Click OK to close this dialog and return to our connection string dialog. See that now our connection string has been filled nicely. Click OK button to save the new connection string and close the dialog.

ADOConn_Dialog_Filled_001.jpg


Set MainConnection's LoginPrompt property to False, since we are not required to supply login credentials when we are accessing this database.


3. Drop A Dataset (TADOTable)


We have setting up our connection correctly, now time to add the first dataset. Click on ADOTable icon on your component pallette (tab ADO) and click again somewhere in your form. Now you have a fresh TADOTable. Name it Table1 for easy reference. Adjust it to use MainConnection by setting its Connection property to MainConnection (Object Inspector provides you with a list, so just select from the list). Now time to tell it which table it should handle. Do it by setting its TableName property into Table1. Again the object inspector will provide you with a list of known tables in the database. So just select from the list.


After done all the above, make sure that Table1 is active by setting its Active property is set to True.


4. Show The Content


Now time to show the content of Table1. For this we need a TDataSource component and a few data controls. TDataSource is required to connect our data controls to our dataset. Open pallette Data Access, click on DataSource icon, and then click somewhere on your form. There you get your fresh TDataSource. Name it Table1Source. Select Table1 for its Dataset property and it's ready.


We want to show the whole content of Table1, so our best choice is to add a TDBGrid to our form. To add TDBGrid, open pallette Data Controls, click on DBGrid icon, then click on your form. Now you have a fresh TDBGrid. Name it grdTable1. Adjust its size and location just like shown in the following image.

FirstDB_Design001.jpg


To connect the TDBGrid to our Table1, all you have to do is to connect it to the TDataSource we created before, Table1Source. So using object inspector set its DataSource property to Table1Source. If you Table1 is active, the DBGrid will immediately filled with data. If not, just activate Table1 (set its Active property to True).


Now you can play around with the DBGrid in design time, like adjusting its size or the width of each column.


5. Run Your First DB Application


Now are you set? Let's run our first db application. Press F9 shortcut key and see your project compiles and run. See, it's easy to create db application with Delphi. Now you can play around with properties of MainConnection, Table1, Table1Source, and grdTable1 to learn how they will affect the project.

FirstDB_Run001.jpg


Full source code of the demo project and the sample database is attached. Feel free to use or improve it. Any kind of feedback or question will be appreciated.


Stay tune, because more tutorials on database with Delphi will follow! Next tutorial would be improving the demo project to be capable of adding new record/data.

Attached Files


  • 3

#2 delphiuser

delphiuser

    CC Newcomer

  • Member
  • PipPip
  • 23 posts
  • Programming Language:Delphi/Object Pascal, Pascal
  • Learning:Delphi/Object Pascal

Posted 12 October 2012 - 02:32 AM

Great tutorial, helped me alot.
  • 0

#3 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,318 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 12 October 2012 - 05:47 AM

Thanks delphiuser, welcome to CodeCall!
  • 0

#4 JabbaWook

JabbaWook

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts
  • Programming Language:Delphi/Object Pascal
  • Learning:Java, C++, JavaScript, Lua, Prolog

Posted 08 December 2012 - 02:27 AM

Hey im new to CodeCall, i found this post incredibly helpful but i was wandering how i can find the next post you talked about? and is there a way to 'follow' you, so i can see future posts? Thanks
  • 0

#5 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,318 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 08 December 2012 - 02:42 AM

Hi JabbaWook, welcome to CodeCall. I am glad you found this thread useful.

By posting in this thread, you will automatically be following it. You will be notified for any future posts.
  • 0

#6 Quintin96

Quintin96

    CC Lurker

  • New Member
  • Pip
  • 8 posts
  • Location:Nelspruit
  • Learning:Java, C#, JavaScript, Delphi/Object Pascal

Posted 17 October 2013 - 08:08 AM

Hi, I have a problem as to working on a database project that has been moved. For instance if I download the project from you and want to continue working on it, the database does not show up - I have repeated step 2 shown here and changed the directory to my new directory where I have placed your project and database. The program runs without giving me errors however the database does not show. 

 

I am a school student and in I.T we constantly need to keep changing the directories of our projects as we have to work at both home and school. A reply will be much appreciated and thanks in advanced!


Nevermind. I feel like such a fool - I found that the problem was my table's Active property was set to False. Thanks for this forum by the way, it was of great help


  • 0

#7 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,318 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 17 October 2013 - 08:52 AM

Hi Quintin, welcome to CodeCall!

Glad to know that you sorted out the problem yourself. Keep us posted with your progress.


  • 0