Microsoft Outlook Outlook Express The Bat! Thunderbird Eudora Opera Web Interface Other
Microsoft Outlook Outlook Express The Bat! Thunderbird Eudora Opera Web Interface Other
Microsoft Outlook
Outlook Express
The Bat!
Thunderbird
Eudora
Opera
Web interface
Other
Vote
Archive of sources
Database
Graphics & Multimedia
Networks & Internet
System
Miscellaneous
Translate to
TO FIND
SEARCH
A R T I C L E S
home
. : Database on typed binary files:.
news
When a programmer, when developing a software product, encounters the need to save data, then
it inevitably raises the question of choosing the type of database: local or multiuser, tables or
Sources DBMS. And, no matter what choice he makes, they all have one absolute advantage: the convenience
of sampling and sorting data. But there are certain and disadvantages.
Components Disk space As you know, the database grows very fast. And not only due to new records, but also
due to the actual not removing the old ones. The records in the tables and DBMS are only marked with
a deletion flag, but they are not physically deleted. Additional tools Part of the tables and DBMS will
Programs not work without installing additional components: server (program) or drivers.
Articles
perform data verification, and the search in the database takes a certain time. Code capacity The
FAQs SQL language is universal and very flexible, but it is also very capacious.
Upload Of course, when it comes to serious programs, all these shortcomings are taken for granted. But if
we need a database in which to simply record and change data without further processing and
outputting the result in the table, then I suggest using binary files. The database on typed binary files,
Search although it is devoid of the charms of the database on tables and DBMS, but also lacks their
disadvantages. As you know, the files take up as much disk space as the text they store. When you
delete a file, the information is deleted immediately and permanently. We do not need any additional
tools to work with such a database, everything is in Delphi. Since we tell the program the name of the
References file with the necessary information, the program does not need to search for it on the entire hard
disk. The program code is minimal,
Site Map For the sample, we'll write a simple program for the library. With it, we will create an electronic
catalog for subscribers and books. Create a new project. The name of the MainForm form. Leave there
DONATE two groupboxes (GroupBox). Arrange them on the right and left side of the form. The left sign
"Subscribers", and the right "Books" (property Caption). Add 6 Labels, 5 Edit, 1 Memo and 5
SpeedButtons from the Additional tab to the groupbox. On the "Books" groupbox, put 7 labels (Label), 7
Edit (Edit), and 5 SpeedButton buttons. Arrange and sign all the elements as follows:
Fig. 1 - Main window of the program
Now create two structures (record) Abonent and Kniga. Here they are:
In this case, the structures are the best suited for our purpose. On the one hand, structures allow
you to collect under the same name a whole list of variables that characterizes one record, which can
be compared with one row in the table. On the other hand, there is no need to write each variable of
the structure separately in a file, we will write the entire structure to the file. In each structure, we will
store data related to the subscriber (TAbonent) and to the book (TKniga). Each subscriber has a
surname (Fio), an address (Adres) and a telephone (Telefon). The Data parameter is the date of
issuing books in the library, Spisok is the list of books that the subscriber took from the library. Each
book can be characterized by the name (Name), author (Avtor), genre (Janr) and accession number
(Nomer). All the books in the library are arranged in sections (Razdel), and they are constantly taken by
subscribers (Abonent). Now create an instance of each structure, as well as variables of typed binary
files:
We created them in the Public section, because our program will have several forms and it is
important that they be visible in each form. Variables of typed files are needed for storing the file
descriptor, separately for subscriber records files and books. In the directory where our program is
located, we will create two folders: "Subscribers" and "Books". In them all our database will be
stored. Now write the code to write the subscriber data into our database. We click on the "Record"
button twice in the "Subscribers" section. In the function of clicking a button, we write the following
code:
In the first line of the code, we specify the "Subscribers" folder as the current directory:
Now references to working with files will mean that the file you are looking for is located exactly in
this folder and nowhere else. Next is the code for filling the structure instance with the data:
Abonent.Fio: = Edit2.Text;
Abonent.Adres: = Edit3.Text;
Abonent.Telefon: = Edit4.Text;
Abonent.Data: = Edit5.Text;
Abonent.Spisok: = Memo1.Text;
Next, we associate the variable of the typed file with a specific file, in other words, we get a
descriptor (reference) to a particular file where we will write the data:
ReWrite (DAbonent);
If such a file already exists, then all information on it will be deleted. As you understand, changing
the information in the file is equivalent to creating a new file.
Next, we write to the file an instance of our structure with data about the subscriber:
CloseFile (DAbonent);
Compile the project. In the field "Name", write: Dmitry Petrov, in the field "Address": Soviet house 6,
field "Phone": 555-555-555, "Book issue date": February 17, 2011, "List": The last of the Mohicans,
Treasure Island, The case of a lame canary. Press the "Record" button and all data will be saved to a
new file "Petrov Dmitry Nikolaevich", which will be created in the "Subscribers" folder, which the
program will issue a corresponding message. Now we can create new subscriber records and edit old
ones. But, to edit the old ones, it would not be bad to find them and view them. We click on the "Find"
button twice, in the "Subscriber" section, and write the following code:
Actually, this can not be called a search. We just check the presence of the file:
CloseFile (DAbonent);
Then we load the data from the structure into the corresponding fields:
Edit2.Text: = Abonent.Fio;
Edit3.Text: = Abonent.Adres;
Edit4.Text: = Abonent.Telefon;
Edit5.Text: = Abonent.Data;
Memo1.Text: = Abonent.Spisok;
And if the desired file does not appear, then we issue a message to the user:
And if all of a sudden we will fall a file that does not contain a structure, or will contain the correct
structure, such as to exclude in any case can not, what would be the program does not generated an
error, we set the trap:
the try
...
...
. ..
except
Exit;
end;
Compile the project, in the field "Search", write: Dmitry Petrov, then click the "Find" button. In the
fields: "Name", "Address", "Phone", "Date of issue of books" and "List", the saved information will be
displayed. In the current version of the program there is only one drawback - it does not allow you to
see the list of all subscribers. So we'll fix it. Create a new form and call it SpisokAbonentForm. We link
the new form with the main form:
implementation
uses Main;
{$ R * .dfm}
On the SpisokAbonentForm form, we throw an instance of the ListView component from the Win32
tab. But before you can use it, you must first configure it:
ViewStile: = vsReport
SortTipe: = stText
GridLines: = True
FlatScrollBars: = True
In the window that appears, click ADD NIW. In the list, a new ListView field appears, mark it, write
"Name" in the Caption property, and in the MaxWidth, MinWidth and Width properties write "300".
Close the list of ListView fields. In the function of activating the SpisokAbonentForm form, write the
following code:
With this code, when you open the SpisokAbonentForm form in ListView, the list of all subscribers
will be loaded. The essence of forming a list of all subscribers is to alternately read the names of files
from the folder "Subscribers" and load them into the ListView. First, we create an instance of the
TSearchRec structure:
var
SearchAbonent: TSearchRec;
It will store all the information about the found file, including its name.
Then we delete everything from the ListView list:
ListView1.Items.Clear;
We set the folder "Subscribers" as the current directory. Then we alternately check all the directory
files. Subscribers:
I ask you to pay attention to the first and second attributes of the FindFirst function:
'*' means that all files with any name and any extension will be searched.
faReadOnly means that only files for reading will participate in the search. If this attribute is replaced
with faAnyFil, then the search will involve not only all files, but also directories, including the top-level
directory, which we do not need at all.
Next, we add the name of the found file to the ListView list:
ListView1.Items.Add.Caption: = SearchAbonent.Name;
Check if there are more files in this directory:
FindClose (SearchAbonent);
end;
Now, in the function of the "List of subscribers" button on the main form, we write the code for
loading the
SpisokAbonentForm window :
We built the list of all subscribers, and in alphabetical order (SortTipe: = stText), but it would not be
bad that the librarian could download a subscriber's data into the program with one double click on the
line with the subscriber's name. For this purpose, in the function onDblClick of our ListView in the form
of SpisokAbonentForm, we write the following code:
The code is known to us so much that it makes no sense to disassemble it. The only thing you need
to pay attention to is the ListView1.Selected.Caption parameter. It stores the record selected from the
ListView list.
Now fill the code with the "Clear" button. It will delete entries from all fields except search:
Here we check the presence of the file to be deleted, and use the DeleteFile (NameFile) function to
delete it. We report all possible results to the user using the ShowMessage function ('message
text'). Here you just need to remember that before you delete a file, you need to download it by
searching, since the name of the file to delete is defined in Edit2.Text.
In order to activate the function of deleting a subscriber, in the function of the "Delete" button of the
"Subscribers" section on the main form, we write the code for loading the DelAbonentForm window:
DelAbonentForm.ShowModal;
end;
The second part of the program, on working with the catalog of books, I will not comment, I will give
only the code itself. This code repeats the code of work with subscribers, the only difference is that the
book data is saved in an instance of another structure:
Kniga: TKniga;
And the code for the button "clear" in the main form:
So we created a program whose database consists of typed binary files. The size of the program
itself, as well as the size of each file with the record is small. Such a program does not burden the
computer either on the resources of the system or on the capacity of the hard disk. Thus, you can
create any other directories, for example, a CD and DVD directory. You can save not only text
information, but also numbers and even pictures or photos. It remains to add one small touch. Look at
the code of our structures:
All data of type String is strictly limited. What is it for. This is a prerequisite for creating a typed file. If
data is specified by a string or a set of characters, then their size must be strictly defined. Try to remove
the length of the string at least one variable, and the compiler will throw an error. As for the final stroke,
it is quite simple. If the variables are limited, then the edits through which the data is entered must also
have an input limit. This can easily be done with the MaxLength property of the edits and memo.