Warning: include(../mainbanner.htm) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/create own database.php on line 34

Warning: include(../mainbanner.htm) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/create own database.php on line 34

Warning: include() [function.include]: Failed opening '../mainbanner.htm' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/makedata/public_html/delphitutorial/create own database.php on line 34

Warning: include(../left.htm) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/create own database.php on line 55

Warning: include(../left.htm) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/create own database.php on line 55

Warning: include() [function.include]: Failed opening '../left.htm' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/makedata/public_html/delphitutorial/create own database.php on line 55
Delphi Tutorial

Step-by-Step

Create Your Own Database

Database is a group of information that have some or many fields, called table, that can do processing data. A lot of available database product in the world start from the legendary of DBASE III until Oracle and free Mysql. However you donot always need big database if you just need some small tables and store private data.

In Delphi, you can create your own simple database. Just have a little knowledge in Pascal programming. Here is some small part that require to build a database.

For example, we write Phone Book application. See picture following above.

Typed File

In Delphi you can find 3 type of files that can be accesed, there is text file, typed-file and untyped-fil. Typed-file is type of file that match to make file of table like create a database file. Typed-file can be perform to define data that you want declare as a record. For example,we will create phone book application where you can store your friend phone numbers. The following declaration creates a record type called TPhone and an array of TMember record variables.
For example,we will create phone book application where you can store your friend phone numbers. The following declaration creates a record type called TPhone and an array of TMember record variables.

  type
TRecord_type = record
field_name_1: field_type_1;
field_name_2: field_type_2;
...
field_name_N: field_type_N;
end;

Then declare the variable file is before you be able to write/read the file, here is code of its declaration.

type
  TPhoneBook = record
    Name: string[25];
    Phone: string[20];
    MobilePhone: string[20];
    Fax: string[20];
  end;
				

Before we can write the information to the disk we have to declare a variable of a file type. The following line of code declares an F file variable

var
  F: File of TRecord_type;

For phonebook example, you declare a variable like following below:

var
  F: File of TPhoneBook;

Note: To create a typed file in Delphi, we use the following syntax:
var SomeTypedFile : file of SomeType
The base type (SomeType) for a file can be a scalar type (like Double), an array type or record type. It should not be long string, dynamic array, class, object or a pointer.
In order to start working with files from Delphi we have to link a file on a disk to a file variable in our program. To create this link we must use AssignFile procedure in order to associate a file on a disk with a file variable.
AssignFile(F, 'Members.dat')
Once the association with an external file is established, the file variable F must be 'opened' to prepare it for reading and/or writing. We call Reset procedure to open an existing file or Rewrite to create a new file. When a program completes processing a file, the file must be closed using the CloseFile procedure. After a file is closed, its associated external file is updated. The file variable can then be associated with another external file.

Create a New Table

First preparation to start to make a database is create a table. In actually, table is a file on disk. Defined your table before what field you want to desire. In this axample, we defined Name, Phone, MobilePhone, and Fax number. see following above:

type
  TPhoneBook = record
    Name: string[25];
    Phone: string[20];
    MobilePhone: string[20];
    Fax: string[20];
  end;
				

Warning: include(../ulink180x90.php) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/create own database.php on line 180

Warning: include(../ulink180x90.php) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/create own database.php on line 180

Warning: include() [function.include]: Failed opening '../ulink180x90.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/makedata/public_html/delphitutorial/create own database.php on line 180

 

Determine your table file name like 'phone.dat'.

Now, declare a variable typed file into
var
F: File of TPhoneBook;

After variable file F declarated, You can create the file by using Rewrite() procedure and close the file which have been made. Well, from now you have a file that called as a table. A new tables ready for use.

const
FTableName = 'phone.dat'; procedure TForm1.BtnCreateTableClick(Sender: TObject);
var
F: File of TPhoneBook;
begin
if not FileExists(FTableName) then
begin
//Try to open the phone.dat
AssignFile(F, FTableName);
try
rewrite(F); //Create "phone.dat" file
finally
CloseFile(F);
end;
end;
...
end;

 

Write to A Database

First job of a database is store information. In this case, data that will be store are name and number phone.

In each a new record always placed in end of line when you insert a record into table. For that reason you must know the last position of record. You may use Filesize() procedure to get that value. And After the position of file known, seek to that position using Seek() procedure to find it.

Now, you can place a new record by using Write() procedure.

Suppose we have filled an array of Delphi members with their names, e-mails and number of posts and we want to store this information in a file on the disk. The following piece of code will do the work:

const
FTableName = 'phone.dat'; procedure TForm1.BtnPostClick(Sender: TObject);
var
F: File of TPhoneBook;
PhoneOwner: TPhoneBook;
begin
//Post record to table
// Try to open the phone.dat file

AssignFile(F, FTableName);
Reset(F);
try
size:= FileSize(F);
seek(F, size);
//Write a couple of telephone records to the file
PhoneOwner.Name:= Edit1.Text;
PhoneOwner.Phone:= Edit2.Text;
PhoneOwner.MobilePhone:= Edit3.Text;
PhoneOwner.Fax:= Edit4.Text;
Write(F, PhoneOwner);
//Close the file
finally
CloseFile(F);
end;
...
end;


Read A Record

After you successfully write data to the table. Once you want to get the exact information that you have saved in file “phone.dat”. In order to retreive all the information from the 'members.dat' file we could use this code:

 

const
FTableName = 'phone.dat'; procedure TForm1.BtnFirstClick(Sender: TObject); var F: File of TPhoneBook; PhoneOwner: TPhoneBook; begin //Reopen the file in read only mode AssignFile(F, FTableName); try Reset(F); if size <> 0 then begin //Set record position to zero Seek(F, 0); Read(F, PhoneOwner); //Display the record contents Edit1.Text := PhoneOwner.Name; Edit2.Text := PhoneOwner.Phone; Edit3.Text := PhoneOwner.MobilePhone; Edit4.Text := PhoneOwner.Fax; end else MessageDlg('No data', mtInformation, [mbOk], 0); finally CloseFile(F); end; ... end;


Note: Eof is the EndOfFile checking function. We use this function to make sure that we are not trying to read beyond the end of the file (beyond the last stored record).

Seeking A Record

Files are normally accessed sequentially. When a file is read using Read() procedure which explained before. The current position of file moves to the next numerically ordered number. Typed files files can also be accessed randomly through Seek() procedure, which moves from current position of file to a specified record.
Go back to the beginning, the first record is current position.
Seek(F, 0) ;

Go to the 12-th record.
Seek(F, 12) ;

Jump to the end of file, the last record is current position.
Seek(F, FileSize(F)) ;

You can use FilePos() function to know where the current position of file.

Update A Record

Sometimes you want to update your phone numbe or any. Facility update will give you to modify data. Before you can change a record, you must defined record position. by using Seek() procedure. And rewrite a available record with Write() procedure.

Procedure UpdateRecord;
var
  ...
begin
  ... //Open file
  try
    Seek(); //find a record to update
    Read(); //displaying to edited VCL like TEdit
    Write(); //Rewrite again a record
  finally
    ... //Close file
end;

procedure TForm1.BtnEditClick(Sender: TObject);
var
F: File of TPhoneBook;
PhoneOwner: TPhoneBook;
begin
if MessageDlg('Are you sure to update this record?',
mtConfirmation, [mbYes, mbNo], 0) = mrYes then
begin
//update open record in database
AssignFile(F, FTableName);
Reset(F); //Reopen the file in read only mode
Seek(F, CurPos);
try
PhoneOwner.Name:= Edit1.Text;
PhoneOwner.Phone:= Edit2.Text;
PhoneOwner.MobilePhone:= Edit3.Text;
PhoneOwner.Fax:= Edit4.Text;
Write(F, PhoneOwner);
finally
CloseFile(F);
end;
end;
end;

Count Records

How many records were available in a file or a table. You can use FileSize() function to get the value.

procedure TForm1.TableSize;
var
F: File of TPhoneBook;
begin
//Try to open the phone.dat
AssignFile(F, 'phone.dat');
Reset(F);
try
RecordCount:= FileSize(F);
//Close the file
finally
CloseFile(F);
end;
end;

 

Bonus: Show About Box

About Box shows some information about your application like title, version etc. And the important thing you can give a link to your website.

User will know your website only by clicking TLabel. To seeing linke a button link, you can change cursor property of TLabel cursor by crHand.Point. Use ShellExecute() command to run the another application like open web browser.tion Dont forget to added ShellAPI unit, because ShellExecute() is a Windows API.

uses ShellAPI;

procedure TForm2.LblLinkClick(Sender: TObject);
begin //Get link to your website
ShellExecute(Handle, 'open', PChar('http://www.makedatabase.com/'), nil, nil, SW_SHOW);
end;

More detail you can download the source code.



Warning: include(../fileroutines.php) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/create own database.php on line 368

Warning: include(../fileroutines.php) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/create own database.php on line 368

Warning: include() [function.include]: Failed opening '../fileroutines.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/makedata/public_html/delphitutorial/create own database.php on line 368

Warning: include(../right.htm) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/create own database.php on line 382

Warning: include(../right.htm) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/create own database.php on line 382

Warning: include() [function.include]: Failed opening '../right.htm' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/makedata/public_html/delphitutorial/create own database.php on line 382

Warning: include(../bottombanner.htm) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/create own database.php on line 397

Warning: include(../bottombanner.htm) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/create own database.php on line 397

Warning: include() [function.include]: Failed opening '../bottombanner.htm' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/makedata/public_html/delphitutorial/create own database.php on line 397