14th Chapter-Simple database application creation and Mastapp Introduction (iii) (3)

Source: Internet
Author: User

14.7 Inserting and deleting records

Although we can insert and delete records in a table using Tdbnavigator in either DBD or in an application form, any important database application completes such operations according to the end user's commands. Similarly, if we have a Field object and its usage, modifying the records in the database, inserting and deleting records will become very easy.

To delete a record in a table, first move the record pointer to the record, and then call the Delete method, so that the record that contains the current pointer is deleted, and we do not have to set the ttable part to edit when we do the delete operation. After the record where the current pointer is deleted, all records below the deleted record move forward, and the record pointer automatically moves to the next record in the record next to the deletion. When you delete a record without reminding the user that you really want to delete the information confirmation box for the current record, be careful when doing so, and if you are developing an application, it is best to provide a confirmation box to ensure that the user does not accidentally delete the record.

Inserting a record is also very simple, Delphi provides two ways for users to insert records into an existing database table by inserting a record at the record where the current record pointer is located, or by inserting a record at the end of the database table. Both of these methods are invoked separately by the Insert method and the Append method. However, whether you call the Insert method or call the Append method to insert a record in the database table with the index, the records added to the index table are written to the database table in the indexed order, which means that the insert and append methods are invoked the same way for the index table. In fact, the Append method applies only to tables that do not have an index, and the table without indexes is not very useful and is not usually created. In almost any case, we insert the record using the Insert method.

Users can generally insert records in two ways: stepping into it begins by creating an empty record, then populating each field of the record, and then writing the record back to disk with three separate steps, and using the Insertrecord method to complete the insert record at a time.

14.7.1 step-by-Step insertion method

The step-by-Step insertion method is divided into three distinct steps: First invoke the Insert method of the ttable part to create a new empty record in ttable, then populate each field of the record, and finally call the Post method to write the new record to the actual database file on disk, before populating and routing the record, It makes no sense to consider where to insert records into a table, assuming that the inserted table is indexed, and that when the Post method is invoked, Delphi automatically inserts the new record inserted into the table in the correct position in the index order. If there is no index in the inserted table, the new record is inserted after the record that contains the current pointer.

Therefore, the program code that inserts records using a step-by-step insertion method is generally the following:

With Table do

Begin

Insert; {Insert a blank record}

< populate each field of the record >

Post {Write the inserted record back to the disk file}

End

For database tables without indexes, you can insert new records into the end of the table using the Append method instead of the Insert method.

14.7.2 Call Insertrecord Insert Record

For a simple application, Delphi allows the user to insert a new record with a single statement, and the new record can have any number of new field values. The Insertrecord method combines the assignment statements and the Psot method calls of the fields in the new record into a single statement.

The Insertrecord method combines each field value of a record into an array of field values as its unique parameter. In an array of field values, you can provide a field value for each field of the inserted record, or assign values from the leftmost column to any number of fields in turn. That is, the user can pass the value of multiple columns to Insertrecord at the same time from the leftmost column of the table until all fields are assigned. The following fields can also be omitted by the user, and Insertrecord fills these unassigned fields with null values. Users can also indicate that the field is empty by passing a reserved word nil to fields that explicitly want to be populated with null values.

If we want to insert a record in the Customer.db table, we can use the following code:

Insertrecord ([' O ', Nil,nil,nil]);

In the above program code, we only filled in four fields: Custno, Company, ADD1, Add2. Insertrecord automatically assigns null values to other fields.

Example 14.7 In this example, we insert and delete records in the Custno.db table, all of which are done in the program instead of using a dbd or data browsing widget.

Insert/Delete Record

List of programs:

Unit TT;

Interface

Uses

Sysutils, Windows, Messages, Classes, Graphics, Controls,

Stdctrls, Forms, Dbctrls, DB, Dbgrids, Buttons, Dbtables, Grids,

Extctrls,mask,dialogs;

Type

TForm1 = Class (Tform)

Dbgrid1:tdbgrid;

Dbnavigator:tdbnavigator;

Panel1:tpanel;

Datasource1:tdatasource;

Panel2:tpanel;

customertable:ttable;

BITBTN1:TBITBTN;

Label1:tlabel;

Label2:tlabel;

BITBTN2:TBITBTN;

BITBTN3:TBITBTN;

Custnoedit:tedit;

Compedit:tedit;

Procedure Formcreate (Sender:tobject);

Procedure Bitbtn2click (Sender:tobject);

Procedure Bitbtn3click (Sender:tobject);

Procedure Formactivate (Sender:tobject);

Private

{Private Declarations}

Public

{Public declarations}

End

Var

Form1:tform1;

Implementation

{$R *. DFM}

Procedure Tform1.formcreate (Sender:tobject);

Begin

Customertable.open;

End

Procedure Tform1.bitbtn2click (Sender:tobject);

Begin

If (Length (Custnoedit.text) =0) and

(Length (Compedit.text) =0)

Then

Messagedlg (' No field value for new record entered! ') mterror,[mbcancel],0

Else

With Customertable do

Begin

indexfieldnames:= ' Custno ';

If FindKey ([Custnoedit.text]) then

Messagedlg (' already exist in this record! ', mterror,[mbcancel],0)

Else

Insertrecord ([Strtoint (Custnoedit.text), Compedit.text,nil]);

Custnoedit.text:= ';

Compedit.text:= ';

End

End

Procedure Tform1.bitbtn3click (Sender:tobject);

Begin

If (Length (Custnoedit.text) =0) and

(Length (Compedit.text) =0)

Then

Messagedlg (' The field value of the record not entered for deletion! ') mterror,[mbcancel],0

Else

With Customertable do

Begin

indexfieldnames:= ' Custno ';

If FindKey ([Custnoedit.text]) then

Begin

If Messagedlg (' Are you sure you want to delete this record?] ', Mtconfirmation,

[mbyes,mbno],0) =mryes then Delete;

End

Else

Messagedlg (' No record you want to delete! '); mterror,[mbcancel],0

Custnoedit.text:= ';

Compedit.text:= ';

End

End

Procedure Tform1.formactivate (Sender:tobject);

Begin

Custnoedit.setfocus;

End

End.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.