RETURN TO INDEX

codiphone.com

BASIC LESSON 6 - GROUP VI 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 sixth group (file management functions)
and all what you can do with them.

This sixth group contains functions
CSVSeparator (6.1)
QuoteSeparator (6.1)
LoadFromFile (6.2)
LoadFromCSVFile (6.2)
LoadFromISAMFile (6.2)
LoadFromMLBFile (6.2)
SaveToFile (6.2)
SaveToCSVFile (6.2)
SaveToISAMFile (6.2)
SaveToMLBFile (6.2)
SaveToExcelFile (6.2)

 

CSVSeparator and QuoteSeparator properties are char or string properties used to read and write CSV (comma separated) and ISAM files. CSVSeparator indicates with what character fields and records are separated. QuoteSeparator is the character around fieldnames and string data in ISAM files.
DELPHI C++
Default
CSVSeparator:
By default CSV data are separated by a ';' char
sample:
to read
field1;field2;field3
use CSVSeparator := ';';

QuoteSeparator:
in ISAM files to read "field1";"field2";"field3";"field4"
QuoteSeparator := '"';

When to change ?
Sometimes fields are separated by simple commas ',', so before using LoadFromFile functions change CSVSeparator.
field1,field2,field3

Samples:
To read the following ISAM file:
'Name','City','Age'
'John','Washington',24
'Andrei','Moscow',41

you need first to initialize
myinstance.CSVSeparator := ',';
myinstance.QuoteSeparator := '''';
Default
CSVSeparator:
CSVSeparator = ";";

QuoteSeparator:
QuoteSeparator = "\"";

Samples:
To read the following CSV file:
Name#City#Age
John#Washington#24
Andrei#Moscow#41

you need first to initialize
myinstance->CSVSeparator = '#';
/*QuoteSeparator is not used because CSV files don't use QuoteSeparator*/
Load functions (LoadFromFile, LoadFromCSVFile, LoadFromISAMFile, LoadFromMLBFile) are used to initialize an mlb table with a file's data.
DELPHI C++
To load a file from its extension:  
use the LoadFromFile API:
This is the easiest way to load a file.
The type of the file (csv, isam, mlb) is defined by its extension.
1- Files of extension .csv are loaded as comma separated files (characters separated by a CSVSeparator char).

2- Files of extension .txt are loaded as ISAM files (strings wrapped around with QuoteSeparator chars, floats without chars around, data separated by a CSVSeparator char).

3- Files of extension .mlb are loaded as mylittlebase files (see mlb file format specification).

For sample:
myinstance.LoadFromFile('file.csv');
myinstance.LoadFromFile('file.txt');
myinstance.LoadFromFile('file.mlb
');


To load any file as a comma separated file:  
sometimes comma separated files (csv) have not a .csv extension. Use the LoadFromCSVFile API to load a file as csv even if its extension is not .csv.

sample:
myinstance.LoadFromCSVFile('data.mlb')
{this sample reads the file as a comma separated file, even if its extension is not .csv}

To load any file as an ISAM file
:  
sample:
myinstance.LoadFromISAMFile('data.mlb')
{this sample reads the file as an ISAM file, even if its extension is not .txt}

To load any file as an ISAM file:  
sample:
myinstance.LoadFromMLBFile('data.txt')
{this sample reads the file as a mylittlebase file, even if its extension is not .mlb}

All these functions return true if the loading is successful, false else.


Sample of ISAM file: An ISAM file is the file for example exported as Text by MS Visual Basic's Data Manager.
It is different from pure CSV files because string data (only strings, not floats or integers) are wrapped around with a QuoteSeparator char which is often a " char:
this is a sample of its structure:
The first row contains fieldnames wrapped and separated by a CSVSeparator (default ;)
"fieldname1";"fieldname2";...

Others rows are:
"string data1";float_data2;...

LoadFromFile:  
For sample:
/*to load a comma separated file*/
myinstance->LoadFromFile("file.csv");


/*to load an ISAM file*/
myinstance->LoadFromFile("file.txt");


/*to load a mlb file*/
myinstance->LoadFromFile("file.mlb
");

To load any file as a comma separated file:  
sample:
myinstance->LoadFromCSVFile("data.any");
/*data.any must have a CSV file's structure*/

To load any file as an ISAM file
:  
sample:
myinstance->LoadFromISAMFile("data.any");
/*data.any must have an ISAM file's structure*/


To load any file as a MLB file:  
sample:
myinstance->LoadFromMLBFile("data.any");
/*data.any must have a MLB file's structure*/




When a table is loaded from a file, it is first automatically cleared with the init API.
That's to say the table loses all its previous data (fields and records), before reading and filling it with file's fields and data.

Don't forget to first save a table's data before loading new data.

All these functions return true if the loading is successful, false else.


Save functions (SaveToFile, SaveToCSVFile, SaveToISAMFile, SaveToMLBFile, SaveToExcelFile) are used to save an mlb table to a file.
DELPHI C++
To save a table to a file formatting it according to its extension:
use the SaveToFile API:
myinstance.SaveToFile('file.csv');
{saves the table to a comma separated file}

myinstance.SaveToFile('file.txt');
{saves the table to an ISAM file}

myinstance.SaveToFile('file.mlb');
{saves the table to a mylittlebase file}

myinstance.SaveToFile('file.xls');
{saves the table to an excel file}

To save a table to any file format:
myinstance.SaveToCSVFile('data.any');
{saves the table to the data.any file as a comma separated file}

myinstance.SaveToISAMFile('data.any');
{saves the table to the data.any file as an ISAM file}

myinstance.SaveToMLBFile('data.any');
{saves the table to the data.any file as a mylittlebase file}

myinstance.SaveToExcelFile('data.any');
{saves the table to the data.any file as an Excel file}

All these functions return true if the saving is successful, false else.
To save a table to a file formatting it according to its extension:
use the SaveToFile API:
myinstance->SaveToFile("file.csv");
/*saves the table to a comma separated file*/

myinstance->SaveToFile("file.txt");
/*saves the table to an ISAM file*/

myinstance->SaveToFile("file.mlb");
/*saves the table to a mylittlebase file*/
/*excel not supported in mlb for C++*/

To save a table to any file format:
myinstance->SaveToCSVFile("data.any");
/*saves the table to the data.any file as a comma separated file*/

myinstance->SaveToISAMFile("data.any");
/*saves the table to the data.any file as an ISAM file*/

myinstance->SaveToMLBFile("data.any");
/*saves the table to the data.any file as a mylittlebase file*/
/*excel not supported in mlb for C++*/


All these functions return true if the saving is successful, false else.

 

 

EXERCISE 6- Use group VI functions to create a new database, initialize it with a comma separated file, and to save it to a mlb file (C++) or an excel file (delphi).
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.LoadFromFile('data1.csv');
    mlb.SaveToFile('data1.xls');
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->LoadFromCSVFile("file1.csv");

    mlb->SaveToFile("file2.mlb");
    mlb->Destroy();
}
GROUP FONCTION 7>>
© ANIROM Multimedia