RETURN TO INDEX
BASIC
LESSON 3 - GROUP III functions
MyLittleBase 2.0 API is divided into 7 groups of
functions
1- Initialization functions
2-Fields functions
3-Rows functions
4-Navigation functions
5-Data management functions
6-File management functions
7-Utils
This lesson deals with the
third group (Rows functions)
and all what you can do with them.
This third group contains functions
AddRow (3.1)
InsertRow (3.2)
RemoveRow (3.3)
RemoveRowByIndex (3.3)
CopyRow (3.4)
CopyRowBySlot (3.4)
PasteRow (3.4)
PasteRowBySlot (3.4)
RowCount (3.5)
GetCurrentRow (3.6)
Remark: k is often used in
this lesson as the index of a row, in mylittlebase the rows are the
rows of the table, and the first row has 1 as index and the last row
has RowCount (number of rows) as index.
So that k>=1 and k<=RowCount.
AddRow, InsertRow and RemoveRow functions are used to
add a new row at the end of a mlb2 table, to insert a row at the
current position of a mlb2 table and to remove (delete) an existing row. |
DELPHI |
C++ |
To add a new row
to a mlb2 table
use the AddRow API:
myinstance.AddRow;
{Adds a new row at the end of the table}
To insert an empty row at the current position
use the go functions ( group IV) to set
the current row, and insert an empty row after or before the current
row by using
myinstance.InsertRow(MLB_AFTER);
or
myinstance.InsertRow(MLB_BEFORE);
{MLB_BEFORE and MLB_AFTER are constants
meaning insert a row before or after the current position.
If the current row is the first row of the
table or if the table is empty, the newly inserted row becomes the
first row of the table.}
To remove the current row
use the RemoveRow API:
myinstance.RemoveRow; {removes the current row
and returns true if it has been well deleted }
To remove a row by specifying its index
use the RemoveRowByIndex API:
myinstance.RemoveRowByIndex(4); {removes the
4th row of the table, returns true if the 4th row exited and has been
deleted, else false} |
To add a new row
to a mlb2 table
use the AddRow API:
myinstance->AddRow();
/*Adds a new row at the end of the table*/
To insert an empty row at the current position
use the gofirst ... functions ( group IV)
to set the current row, and insert an empty row after or before the
current row by using
myinstance->InsertRow(MLB_AFTER);
or
myinstance->InsertRow(MLB_BEFORE);
/*MLB_BEFORE and MLB_AFTER are constants
meaning insert a row before or after the current position.
If the current row is the first row of the
table or if the table is empty, the newly inserted row becomes the
first row of the table.*/
To remove the current row
use the RemoveRow API:
myinstance->RemoveRow(); /*removes the
current row and returns true if it has been well deleted*/
To remove a row by specifying its index
use the RemoveRowByIndex API:
myinstance->RemoveRowByIndex(4); /*removes
the 4th row of the table, returns true if the 4th row exited and has
been deleted, false either*/ |
CopyRow, CopyRowBySlot, PasteRow,
PasteRowBySlot are used to copy and paste rows. |
DELPHI |
C++ |
To copy a row to
memory
use the CopyRow API:
myinstance.CopyRow;
{copies all fields' data of a row to memory};
or
myinstance.CopyRowBySlot(1);
{use CopyRowBySlot to copy two rows at the
same moment}
{for sample you want to copy the first row in
the slot 0 and the 4th row in the slot 1, you must use the code below
to do that}
myinstance.GoFirst;
myinstance.CopyRowBySlot(0);
myinstance.Go(4);
myinstance.CopyRowBySlot(1);
{MyLittleBase 2.0 has got only 2 slots, 0 and 1}
To paste a row
from memory
use the PasteRow API:
myinstance.PasteRow;
{pastes the row previously copied by CopyRow,
CopyRow is the same as CopyRowBySlot(0) and PasteRow is the same as
PasteRowBySlot(0)}
or
myinstance.PasteRowBySlot(0);
{pastes the row (all fields' data) contained
into the slot 0, into the current selected row (with go functions),
replacing any previous data};
All functions return true is success, else
false. |
To copy a row to
memory
use the CopyRow API:
myinstance->CopyRow();
or
myinstance->CopyRowBySlot(1);
To paste a row from memory
use the PasteRow API:
myinstance->PasteRow();
or
myinstance->PasteRowBySlot(0);
sample:
copy the 7th row and paste it in the 3rd row:
myinstance->Go(7);
myinstance->CopyRow();
myinstance->Go(3);
myinstance->PasteRow();
|
RowCount returns the number of rows in the
table and GetCurrentRow gets the number of the current row.
GetCurrentRow returns 1 for the first row, and RowCount for the last
row. |
DELPHI |
C++ |
To get number of
rows of the table
call the RowCount function:
Caption := IntToStr(myinstance.RowCount);
To get the number of the current row
call the GetCurrentRow API:
Caption := IntToStr(myinstance.GetCurrentRow);
To set the name of the field number k
sample: myinstance.GoFirst;
Caption := IntToStr(myinstance.GetCurrentRow);
Caption must print 1 |
To get number of
rows of the table
call the RowCount function:
printf("%d", myinstance->RowCount());
To get the number of the current row
call the GetCurrentRow API:
printf("%d", myinstance->GetCurrentRow());
To set the name of the field number k
sample: myinstance->Go(11);
printf("%d", myinstance.GetCurrentRow);
Must print 11 if success, 0 else (table empty
...). |
EXERCISE 3- Use group III functions
first to Add create the database, second to add 2 fields Name and
Address, third to add 4 new rows, then exchange the first row and last
row data. |
DELPHI |
C++ |
unit Unit1;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, mlb2;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
public
end;
var
Form1: TForm1;
mlb: TMlb2;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
var i: integer;
begin
{creates the database}
mlb := TMlb2.Create;
{adds the fields}
mlb.AddField('Name');
mlb.AddField('Address');
{adds the 4 rows}
for i:=1 to 4 do
mlb.AddRow;
{copies the first
row to slot 0}
mlb.GoFirst;
mlb.CopyRow;
{copies the last row
to slot 1}
mlb.GoLast;
mlb.CopyRowBySlot(1);
{pastes the slot 0
to the last row}
mlb.PasteRowBySlot(0);
{pastes the slot 1
to the first row}
mlb.GoFirst;
mlb.PasteRowBySlot(1);
{first and last
rows' data are exchanged};
end;
procedure
TForm1.FormDestroy(Sender: TObject);
begin
mlb.Free;
end;
end.
|
#include <stdio.h>;
#include "mlb2.h";
int main(int argc, char* argv[]) {
int i;
TMlb2* mlb =
mlb->Create();
mlb := TMlb2.Create;
mlb->AddField("Name");
mlb->AddField("Address");
for (i=0; i<4;
i++) mlb->AddRow();
mlb->GoFirst();
mlb->CopyRowBySlot(0);
mlb->GoLast();
mlb->CopyRowBySlot(1);
mlb->PasteRow();
mlb->GoFirst();
mlb->PasteRowBySlot(1);
printf("%d",
mlb->RowCount());
/*must print 4*/
/*delete row 1*/
mlb->GoFirst(); mlb->RemoveRow();
printf("%d",
mlb->RowCount());
/*must print 3*/
mlb->Destroy();
}
|
|
|
|