C++Builder - Database tutorial, part 1;TerminologyA database is an organized collection of information.
A table contains records, and each record contains fields. In other words, each column in the table is a field, and each row is a record. A query is a command that retrieves data from the database. Text-databaseIn this tutorial, we will build a flat text database for maintaining the stock of articles of a furniture shop.
Compilation and testPreparations
Compilation
You'll note that the program is not very user friendly yet. We'll change that in the next parts of our tutorial. Organisation of the database and setupFor each record, we create an object of the type TArticle. When you look at the source code in Unit1.cpp, at the beginning you see the declaration of our class TArticle: class TArticle : public TObject { private: String FName; float FPrice; int FStock; public: __property String Name = { read = FName, write = FName }; __property float Price = { read = FPrice, write = FPrice }; __property int Stock = { read = FStock, write = FStock }; }; We shall store the data in a TStringList, named Articles. We declared stringlist Articles as a private member of the form, as can be seen in Unit1.h: Articles is created at the beginning of the application, see Unit1.cpp: void __fastcall TForm1::FormCreate(TObject *Sender) { Articles = new TStringList; Articles->CaseSensitive = false; Articles->Sorted = true; // automatically free the stringlist's objects when it is destroyed Articles->OwnsObjects = true; // Some housekeeping: edID->Text = ""; edName->Text = ""; edPrice->Text = ""; edStock->Text = ""; edFind->Text = ""; stStatus->Caption = ""; } Articles is destroyed at the end of the application, see Unit1.cpp:
User interfaceA click on btnAdd prepares for adding an article: void __fastcall TForm1::btnAddClick(TObject *Sender)
{
stStatus->Caption = " Adding Article ";
edID->Text = "";
edName->Text = "";
edPrice->Text = "";
edStock->Text = "";
edID->SetFocus(); // position the cursor in edit box edID
}
Clicking btnOK stores the data, if an article with the same ID does not already exist. Else, we inform the user that an Article with this ID already exists: void __fastcall TForm1::btnOKClick(TObject *Sender)
{
int index;
String ID;
ID = edID->Text;
TArticle *Art = new TArticle; // create Art, a TArticle object
if (Articles->Find(ID, index))
stStatus->Caption = " " + ID + " already exists ";
else {
Art->Name = edName->Text;
Art->Price = StrToFloat(edPrice->Text);
Art->Stock = StrToInt(edStock->Text);
Articles->AddObject(ID, Art);
stStatus->Caption = " Article was added ";
}
}
A preliminary check for doubles is done with: Storing the data is done in this line: Clicking btnCancel simply informs the user that nothing was added: Finally, btnFind is used for finding an article, given its ID: void __fastcall TForm1::btnFindClick(TObject *Sender)
{
int index;
String ID;
ID = edFind->Text; // input the ID from edit box edFind
if (Articles->Find(ID, index)) { // if found, the position is returned in variable index
stStatus->Caption = " Article was found ";
TArticle *Art = static_cast<tarticle*>(Articles->Objects[index]);
edID->Text = ID;
edName->Text = Art->Name;
edPrice->Text = FormatFloat("0.00", Art->Price);
edStock->Text = IntToStr(Art->Stock);
}
else
stStatus->Caption = " Article " + ID + " not found ";
}
Important note: directly using Objects[index] will not work! |
Table of contents
1. Text Database 2. DB Browser 3. Edit and Delete 4. Load / Save 5. Validating See also: ClientDataSet and XML
|