|
RETURN TO INDEX
codiphone.com
BASIC
LESSON 5 - GROUP V 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
fifth group (data management functions)
and all what you can do with them.
This fifth group contains functions
IsEmpty (5.1)
GetData (5.2)
GetDataByIndex (5.2)
GetFloat(5.2)
GetFloatByIndex (5.2)
AccessData (5.2 , 5.3)
SetData (5.3)
SetDataByIndex (5.3)
SetFloat (5.3)
SetFloatByIndex (5.3)
| IsEmpty just tests if the table is empty or not
(contains rows or not). |
| DELPHI |
C++ |
To test if the
table is empty
use the BeginningOfFile property:
r := myinstance.IsEmpty;
{same as r := myinstance.RowCount=0;}
|
To test if the
table is empty
use the BeginningOfFile property:
r = myinstance->IsEmpty();
{same as r = myinstance->RowCount()==0;} |
| |
|
| Get functions (GetData, GetDataByIndex, GetFloat, GetFloatByIndex,
AccessData array) are used to read
a record's value in the table, accessing it from the current row and
from the field's name, or index, or the coordinates of the matrix (the
table is then seen as a matrix). |
| DELPHI |
C++ |
To get floats or
string data by a field's name:
use the GetFloat or GetData API:
myinstance.GoFirst;
Result := myinstance.GetFloat('price');
Caption := myinstance.GetData('address');
{This sample returns the price of the first
row of the table, and sets the form's caption to the address of the
first row}
To get floats or
string data by a field's index:
use the GetFloatByIndex or
GetDataIndex API:
var r: integer;
s: string;
if myinstance.Go(4) then begin
{if the 4th row exists}
r := trunc(myinstance.GetFloatByIndex(2));
s := myinstance.GetDataByIndex(4);
{integer r receives the second's field's value
of the 4th row, and string s receives the 4th field's value of the 4th
row}
end else begin
end;
To table data as
a matrix
use AccessData array:
Caption := myinstance.AccessData[4, 1];
{Caption receives the value of the first row
(1) and the 4th field (4)}
{AccessData[field_index, row_index] returns
only to strings, even if values are floats} |
To get floats or
string data by a field's name:
use the GetFloat or GetData API:
int r;
myinstance->GoLast();
r = (int)myinstance->GetFloat("price");
myinstance->Go(7);
printf("%s",
myinstance->GetData("address"));
/*This sample returns the price of the last
row of the table, and the address of the 7th row*/
To get floats or
string data by a field's index:
use the GetFloatByIndex or
GetDataIndex API:
int r;
char* s;
if (myinstance->Go(4)) {
/*if the 4th row exists*/
r = (int)myinstance->GetFloatByIndex(2);
s = myinstance->GetDataByIndex(4);
/*integer r receives the second's field's
value of the 4th row, and string s receives the 4th field's value of
the 4th row*/
}else {
}
/*No AccessData[field_index, row_index] in C++
version*/
|
| Set functions (SetData, SetDataByIndex, SetFloat, SetFloatByIndex,
AccessData array) are used to
write a record's value in the table, accessing it from the current row
and from the field's name, or index, or the coordinates of the matrix
(the table is then seen as a matrix). |
| DELPHI |
C++ |
To set floats or
string data by a field's name:
use the SetFloat or SetData API:
myinstance.GoFirst;
myinstance.GoNext;
myinstance.SetFloat('price', 1000);
myinstance.SetData('address', 'washington
avenue');
{This sample sets the price of the second row
of the table to $1000, and sets the address of the second row}
To set floats or
string data by a field's index:
use the GetFloatByIndex or
GetDataIndex API:
myinstance.GoFirst;
myinstance.GoNext;
myinstance.SetFloatByIndex(2, 12500.5);
myinstance.SetDataByIndex(6, 'washington
avenue');
{This sample sets the price (field number 2)
of the second row of the table to $12500.5, and sets the address (6th
field) of the second row}
{AccessData matrix is read only and
cannot be used to set data} |
To set floats or
string data by a field's name:
use the SetFloat or SetData API:
myinstance->GoFirst();
myinstance->GoNext();
myinstance->SetFloat("price", 1000);
myinstance->SetData("address", "washington
avenue");
To set floats or
string data by a field's index:
use the GetFloatByIndex or
GetDataIndex API:
myinstance->GoFirst();
myinstance->GoNext();
myinstance->SetFloatByIndex(2, 12500.5);
myinstance->SetDataByIndex(6, "washington
avenue");
|
| EXERCISE 5- Use group V functions to
create a new database and get and set records. |
| 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);
begin
mlb := TMlb2.Create;
{create 2 fields}
mlb.AddField('Name');
mlb.AddField('Age');
{add 3 rows}
mlb.AddRow;
mlb.AddRow;
mlb.AddRow;
{set row's data}
mlb.GoFirst;
mlb.SetData('Name', 'Alfred');
mlb.SetFloat('Age', 26);
mlb.GoNext;
mlb.SetData('Name', 'Frederica');
mlb.SetFloat('Age', 24);
mlb.GoNext;
mlb.SetData('Name', 'Ryan');
mlb.SetFloat('Age', 57);
Caption := mlb.AccessData[1, 3];
{caption displays 'Ryan'}
end;
procedure
TForm1.FormDestroy(Sender: TObject);
begin
mlb.Free;
end;
end.
|
#include <stdio.h>;
#include
"mlb2.h";
int main(int argc, char* argv[]) {
TMlb2* mlb = mlb->Create();
mlb->AddField("Name");
mlb->AddField("Age");
mlb->AddRow;
mlb->AddRow;
mlb->AddRow;
mlb->GoFirst();
mlb->SetData("Name", "Alfred");
mlb->SetFloat("Age", 26);
mlb->GoNext();
mlb->SetData("Name",
"Frederica");
mlb->SetFloat("Age", 24);
mlb->GoLast();
mlb->SetData("Name", "Ryan");
mlb->SetFloat("Age", 57);
printf("%d", GetDataByIndex(1));
/*prints "Ryan"*/
mlb->Destroy();
} |
|