Linux Programming-Database (chapter seventh)

Source: Internet
Author: User

7.3 Database This is the DBM database for Linux, and the code is downloaded in dbm database code. You can use files to store data, why do you need a database? Because in some cases, the characteristics of the database provide a better way to solve the problem. There are two advantages to using a database compared to using files to store data:
1. Can store variable-length data records, which is difficult to implement for flat, unstructured files
2. The database uses indexes to efficiently store and retrieve data. One notable advantage of this is that the index does not necessarily have to be a simple record number--which is easy to implement in a flat file, which can be an arbitrary string.
All versions of Linux and most Unix versions of the 7.3.1 DBM database are shipped with the system with a basic, but very efficient set of data storage routines, which is a dbm database. dbm Database is suitable for storing relatively static indexed data
1.DBM Introduction
Although some free relational databases, such as MySQL and PostgreSQL, are used more and more widely, the DBM database still plays an important role in Linux. Those Linux distributions that use RPM are used to store information about installed packages in dbm. Compared with a more complete database product like MySQL, the advantage of dbm is that it is a lightweight software and is very easy to compile into a binary file that can be published because it does not need to install a separate database server.
The DBM database can use indexes to store variable-length data structures, and then scan the database by index or sequentially to retrieve the structure. The DBM database is designed to handle data that is frequently accessed but is seldom updated. because they are very slow to create data items and are very fast to retrieve
2. Get dbm
Most major Linux distributions will install GDBM by default, but in some distributions it may be necessary to use Software Manager to install the appropriate development library, for example, in Ubuntu, you may need to install the Libgdbm-dev package.
3. Troubleshooting and Reloading dbm
If the system already has GDBM installed and the NDBM compatibility mode is supported by default, you need to do the following:
1-Include the header file in the C source file ndbm.h
2-Use the Compile line option-i/usr/include/gdbm to include the header file directory/usr/include/gdbm
3--lgdbm Link gdbm library with compile line options
If this does not work, you need to link the compatibility library before linking the main library, as follows:
1-Include header file in C source file gdbm-ndbm.h instead of ndbm.h
2-Use the Compile line option-I/USR/INCLUDE/GDBM contains the header file directory/usr/include/gdbm
3-Link Other GDBM compatible libraries using the Compile line option-lgdbm_compat-lgdbm

The 7.3.2 dbm routine dbm header files and library files must be linked in when the program is compiled. Use the option-lgdbm in the compile line to link the implementation.
The basic element of a dbm database is the block of data that needs to be stored and the data block associated with it that is used as a keyword when retrieving data. Each dbm chunk must have a unique keyword for each block of data to be stored. The value of the keyword is used as the index to store the data. DBM has no restrictions on keywords and data, and no errors are defined for the use of extra-long keywords and data.
To manipulate these data blocks, a new data type named Datum is defined in header file Ndbm.h (gdbm.h), and the exact content of the type depends on the implementation, which contains at least two members:
void *dptr;
size_t Dsize;
Datum is a type defined with a typedef statement, and a type definition is declared for dbm in the ndbm.h file, which is a structure used to access the database, similar to the file structure used to access the files. The internal structure defined by the DBM type is dependent on the implementation, and it is never allowed to be used directly.
When using the DBM Library, if you are referencing a block of data, you must declare a variable of type datum, point the member Dptr to the starting point of the data, and set the member dsize to the length of the containing data. Either the data to be stored or the index used to access it is referenced by this datum type.
When you open a dbm database, you typically create two physical files, the suffix is. Pag and. Dir, and returns a dbm pointer that is used to access two files. Access to them can only be done through the dbm routines.
The primary dbm function for the 7.3.3 dbm Access function is prototyped as follows:
#include <ndbm.h>
DBM *dbm_open (const char *filename, int file_open_flags, mode_t file_mode);
int Dbm_store (dbm *database_descriptor, Datum key, datum content, int store_mode);
Datum dbm_fetch (dbm *datebase_descriptor, Datum key);
void Dbm_close (dbm *database_descriptor);
1.dbm_open function
This function is used to open an existing database or to create a new database, and the filename parameter is a basic file name that does not contain the. dir or. Pag suffix. The second parameter controls the read, write, and read/write permissions for the database. If you are creating a new database, this flag must be binary with O_creat or allow the file to be created. The third parameter specifies the initial permissions for the file that will be created.
Dbm_open returns a pointer to the DBM type that will be used for all subsequent access to the database, and if it fails, it returns (dbm*) 0
2.dbm_store function
The Dbm_store function stores data in a database, and all data must be stored with a unique index, in order to define the data to be stored and the indexes used to reference it, you must set the parameters of two datum types: one for the reference index and one for the actual data. The last parameter, Store_mode, is used to control what happens when an attempt is made to store data in an existing keyword. If it is set to Dbm_insert, the store operation fails and Dbm_store returns 1. If it is set to Dbm_replace, the new data overwrites the existing data and Dbm_store returns 0. When other errors occur, Dbm_store returns a negative value.
3.dbm_fetch function
The Dbm_fetch function is used to retrieve data from the database. It uses a pointer returned by a previous Dbm_open call and a datum type structure that points to the keyword as its argument. It returns a structure of type datum. If the data associated with this keyword is found in the database, the values of the dptr and Dsize members of the Returned datum structure are set to the values of the corresponding data. If no keyword is found, dptr will be set to null.
4.dbm_close function
This function closes the database opened with Dbm_open, and its parameters are the dbm pointers returned by the previous Dbm_open call.
Write a dbm program dbm1.c
First, open the database and, if necessary, create it, then populate the 3 members of the Items_to_store as the test data. For each member, create an index keyword (using the first character of two strings plus an integer). Then, set up two datum structures, one for the keyword and another for the stored data. After storing the 3 data items in the database, build a new keyword and set a datum structure to point to it. Then, use this keyword to retrieve data from the database. Determines whether the retrieval was successful by examining whether the DPTR member in the returned datum structure is null.
7.3.4 other dbm functions int dbm_delete (dbm *database_descriptor, Datum key);
int Dbm_error (dbm *database_descriptor);
int Dbm_clearerr (dbm *database_descriptor);
Datum dbm_firstkey (dbm *database_descriptor);
Datum dbm_nextkey (dbm *database_descriptor);
1.dbm_delete function
Used to delete a data item from the database, which uses a datum type structure that points to the keyword as its argument.
2.dbm_error function
Used to test for errors occurring in the database.
3.dbm_clearerr function
Used to clear all the error condition flags in the database that have been placed
4.dbm_firstkey and Dbm_nextkey functions
These two functions are typically used to scan all the keywords in the database. The loop structure they need is as follows:
DBM *db_ptr;
Datum key;
for (key = Dbm_firstkey (db_ptr); key.dptr; key = Dbm_nextkey (db_ptr));
Writing a program dbm2.c

Linux Programming-Database (chapter seventh)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.