Warning: include(../mainbanner.htm) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/delete a record.php on line 35

Warning: include(../mainbanner.htm) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/delete a record.php on line 35

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/delete a record.php on line 35

Warning: include(../left.htm) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/delete a record.php on line 56

Warning: include(../left.htm) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/delete a record.php on line 56

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/delete a record.php on line 56
Delphi Tutorial

Delete A Record

In basic tutorial with title Create Your Own Database less be complete because don't have delete a record function. This is the next tutorial explained how to your database software almost same like the other database software.

Deleting a record in a tables isnot easy work like you add a record. Because Delphi not givien delete a record procedure. But you can do it if you have be able to handle of file by byte per byte, delete byte carefully and compile back records in the table.

Other things which I know that many standard database not really do delete record but they make hide it from user. With a litle trick you can make a record was delete.

I divided into four section

Add Delete Sign

Here, I'm also do not displaying a record which have been deleted, in fact just make it hide:-) I use the application of Ghost Email Book for example which you can learn. First, add field Hide with boolean type. Field Hide represent as the delete sign

type
  TEmailBook = record
    NumId: longint;
    Title: string[30];
    Email: string[60];
    Username: string[25];
    Password: string[25];
    Website: string[40];
    Note: string[255];
    Hide: boolean;
  end;

Warning: include(../ulink180x90.php) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/delete a record.php on line 140

Warning: include(../ulink180x90.php) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/delete a record.php on line 140

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/delete a record.php on line 140

Insert A New Record

In every addition a record, value of Hide field always filled by false to indicate that the record is available or not deleted.

procedure TForm1.WriteToTable(FTitle,FEmail,FUser,
 FPassword,FWebsite,FNote: string; FHide: Boolean);
var
  F: File of TEmailBook;
  Gmail: TEmailBook;
  Size: longint;
begin
  //write to database
  //Try to open the file for writing to
  AssignFile(F, DBFilename);
   Reset(F);
   try
    //Set file position to the end of position	
    Size:= FileSize(F);
    seek(F, size);
    //Write a couple of email records to the file
    Gmail.NumId:= Size;
    Gmail.Title:= FTitle;
    ...
    Gmail.Hide:= FHide;
    Write(F, Gmail);
   //Close the file
   finally
    CloseFile(F);
   end;
end;

procedure TForm1.BtnPostClick(Sender: TObject);
begin
 ...
  if InsertRec then
  begin
    //Add a record into databse
    WriteToTable(Edit1.Text, Edit2.Text, Edit3.Text,
     Edit4.Text, Edit5.Text, Edit6.Text, false);
    InsertRec:= false;
  end;
  ...
end;

Hide A Record As Delete A Record

A available record which want to delete. Delete routine is same like you want to update a record. But only change value of Hide field turn into true. Hide is true, it's mean a record is deleted.

procedure TForm1.UpdateTable(FTitle,FEmail,FUser,
FPassword,FWeb,FNote: string; FHide: boolean);
var
  F: File of TEmailBook;
  Gmail: TEmailBook;
begin
  //update table
  //Reopen the file in read only mode
  AssignFile(F, DBFilename);
  Reset(F);
  //Display a record
  Seek(F, RecPosition);
  try
    Gmail.title:= FTitle;
    ...
    Gmail.Hide:= FHide;
    Write(F, Gmail);
  finally
    CloseFile(F);
  end;
end;

procedure TForm1.BtnDeleteClick(Sender: TObject);
begin
  if MessageDlg(
  'Are you sure want to delete this record?',
   mtConfirmation, [mbYes, mbNo], 0) = mrYes then
  begin
    //update open record in database
    UpdateTable(Edit1.Text, Edit2.Text, Edit3.Text,
     Edit4.Text, Edit5.Text, Edit6.Text, true);
    ...
  end;
end;

Read A Undelete Record

Donot displaying the record which have been deleted. If Hide is true, make record is not show and the record will be show when Hide is false.

procedure TForm1.ReadFromTable(CurPos: longint;
 prior: boolean);
var
  F: File of TEmailBook;
  Gmail: TEmailBook;
begin
  //Read from table
  //Reopen the file in read only mode
  AssignFile(F, DBFilename);
  Reset(F);
  if FileSize(F) = 0 then exit;
  //Display a record from file
  try
    Repeat
      Seek(F, CurPos);
      Read(F, Gmail);
      if not Gmail.Hide then
      begin
        Edit1.Text:= Gmail.Title;
        Edit2.Text:= Gmail.Email;
        Edit3.Text:= Gmail.Username;
        Edit4.Text:= Gmail.Password;
        Edit5.Text:= Gmail.Website;
        Edit6.Text:= Gmail.Note;
      end else
      if prior then
        dec(CurPos)
      else
        inc(CurPos);
    until (Gmail.hide = false) or EOF(F);
    RecPosition:= FilePos(F)-1;
  //Close the file for the last time
  finally
    CloseFile(F);
  end;
end;

Now, your own database more better than before.



Warning: include(../fileroutines.php) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/delete a record.php on line 300

Warning: include(../fileroutines.php) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/delete a record.php on line 300

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/delete a record.php on line 300

Warning: include(../right.htm) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/delete a record.php on line 315

Warning: include(../right.htm) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/delete a record.php on line 315

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/delete a record.php on line 315

Warning: include(../bottombanner.htm) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/delete a record.php on line 330

Warning: include(../bottombanner.htm) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/delete a record.php on line 330

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/delete a record.php on line 330