RETURN TO INDEX
codiphone.com
BASIC LESSON 2 - GROUP II 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
second group (Fields functions)
and all what you can do with them.
This second group contains functions
AddField (2.1)
RemoveField (2.2)
DataType (2.3)
GetFieldType (2.3)
GetFieldTypeByIndex (2.3)
SetFieldType (2.3)
SetFieldTypeByIndex (2.3)
GetFieldName (2.4)
GetFieldIndex (2.4)
FieldName (2.4)
FieldCount (2.5)
Remark: k is often used in
this lesson as the index of a field, in mylittlebase the fields are the
columns of the table, and the first column has 1 as index and the last
column has FieldCount (number of fields) as index.
So that k>=1 and k<=FieldCount.
AddField and RemoveField functions are used to add new
fields (column) to the mlb2 table and to remove (delete) an existing
field. |
DELPHI |
C++ |
To add a new field
to a mlb2 table
use the AddField API:
myinstance.AddField('FIELDNAME');
{Adds a the field FieldName to the table, and
the count of fields (FieldCount) is incremented, this field's index is
the new FieldCount.}
To remove (delete) a field from the table
myinstance.RemoveField('FIELDNAME');
{deletes the field named FIELDNAME}
or
myinstance.RemoveField(FieldName[k]);
{deletes the field number k} |
To add a new field
to a mlb2 table
use the AddField API:
myinstance->AddField("FIELDNAME");
To remove (delete) a field from the table
myinstance->RemoveField("FIELDNAME");
{deletes the field named FIELDNAME}
or
myinstance->RemoveField(GetFieldName(k));
{deletes the field number k} |
DataType, GetFieldType,
GetFieldTypeByIndex, SetFieldType, SetFieldTypeByIndex deals
with fields' type of data (Float, String, or Unknown).
MLB Data can be floats or strings, a field's
type of data is string by default, but can be changed to float, or
stored as float in ISAM or MLB files.
These type functions permit to get and set the
type of data of each field. |
DELPHI |
C++ |
To get a field's
type of data
use the FieldType property:
Caption := myinstance.FieldType[k];
Caption :=
myinstance.FieldType[myinstance.GetFieldIndex('NAME')];
To set a field's
type of data
use the FieldType property:
myinstance.FieldType[k] := 'FLOAT';
myinstance.FieldType[myinstance.GetFieldIndex('NAME')]
:= 'STRING';
|
To get a field's
type of data
call the GetFieldType API:
printf("%s",
myinstance->GetFieldType("NAME"));
or
call the GetFieldTypeByIndex API:
printf("%s",
myinstance->GetFieldTypeByIndex(k));
To set a field's
type of data
call the SetFieldType API:
myinstance->SetFieldType("FIELDNAME",
"FLOAT");
or
call the SetFieldTypeByIndex API:
myinstance->SetFieldTypeByIndex(k,
"STRING"); |
GetFieldName, GetFieldIndex, FieldName
methods are used to get and set informations about fields.
FieldCount gets the number of fields in the
table. |
DELPHI |
C++ |
To get the name of
the field number k (k>=1 and k<=FieldCount)
call the GetFieldName API:
Caption := myinstance.GetFieldName(k);
or
use the FieldName property:
Caption := myinstance.FieldName[k];
To get the index of a field name
call the GetFieldIndex API:
Caption :=
IntToStr(myinstance.GetFieldIndex('NAME'));
To set the name of the field number k
use the FieldName property:
myinstance.FieldName[k] := 'NEWNAME'; |
To get the name of
the field number k
call the GetFieldName API:
printf("%s", myinstance->GetFieldName(k));
To get the index of a field name
call the GetFieldIndex API:
printf("%d",
myinstance->GetFieldIndex("NAME"));
To set the name of the field number k
use the SetFieldName API:
myinstance->SetFieldName(k, "NEWNAME"); |
EXERCISE 2- Use group II functions to
Add 3 fields to a new mlb2 object, to change the second field's name
to "COMPANY", to Remove the last field and to print the number of
fields. |
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;
mlb.AddField('FIRST
FIELD');
mlb.AddField('SECOND
FIELD');
mlb.AddField('THIRD
FIELD');
mlb.FieldName[2] :=
'COMPANY';
mlb.RemoveField(mlb.FieldName[mlb.FieldCount]);
Caption :=
IntToStr(mlb.FieldCount);
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 := TMlb2.Create;
mlb->AddField("FIRST FIELD");
mlb->AddField("SECOND FIELD");
mlb->AddField("THIRD FIELD");
mlb->SetFieldName(mlb->GetFieldName(2), "COMPANY");
mlb->RemoveField(mlb->GetFieldName(mlb->FieldCount()));
printf("%d",
mlb->FieldCount());
mlb->Destroy();
}
|
|