Record operations
class db
{
bool record_exist(string const & table_name, int record_number);
int get_number_records(string const & table_name);
void delete_record(string const & table_name, int record_number);
}
class db //equivalent methods with indexes:
{
bool record_exist(int table_number, int record_number);
int get_number_records(int table_number);
void delete_record(int table_number, int record_number);
}
Sample
int nb_records=mydb.get_number_records("company");
for (int i=0;i<nb_records;i++)
{
if (mydb.record_exist("company", i))
mydb.delete_record("company", i);
}
bool record_exist(string const & table_name, int record_number)
bool record_exist(int table_number, int record_number)
This method returns true if a given record exists, false otherwise (the record has not been created or has been deleted). Please note that you can delete a record, however, the index of the other records is not affected by a deletion. This enables you not to reindex all the data (notably in your gui). If you are sure that the record exists (for instance if you take the index of a record in a list of items, then the selected item exists and it is not necessary to call this method).
//let's say we are sure the record 0 is created
//but we don't know if the record 1 exists...
if (mydb.record_exist("company", 1))
string myname=mydb.get_field_string("company", 1, "name");
else
...//error: the record does not exist.
int get_number_records(string const & table_name)
int get_number_records(int table_number)
This method returns the number of records of a given table.
Important note: even if you delete some records, the total number is not affected. In other words, if you create 10 records and delete 3, this method will return 10 (when there are really only 7 records). That's why you need to use record_exist in order to verify that a record exists before using it...
int nb_records=mydb.get_number_records("company");
for (int i=0;i<nb_records;i++)
{
if (mydb.record_exist("company", i))
...
else
...
}
void delete_record(string const & table_name, int record_number)
void delete_record(int table_number, int record_number)
This method deletes a given record. Please note that the index of the other records is not affected.
Important note: if you delete a record which has "children", all the children elements will be deleted. You need to take it into account for your data model, as well as your gui...
//let's suppose you have record 0 and record 1 created in table "company"
mydb.delete_record("company", 0);
bool test=mydb.record_exist("company", 0);//false
bool othertest=mydb.record_exist("company", 1);//true;
Beware of this method, if your table has children, all the records of the child table will be deleted. Shall you need more information, please see help on relations.
|