Open source embedded database BerkeleyDB (2)

Source: Internet
Author: User
Article title: open-source embedded database BerkeleyDB (2 ). Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
   Unified Application Programming Interface
Using the functions provided by Berkeley DB for database access and management is not complicated. In most cases, you only need to call according to the unified interface standards to complete the most basic operations.
  
   Open Database
Generally, there are two steps to open a database: first call the db_create () function to create an instance of the DB structure, and then call the DB-> open () function to complete the real open operation. Berkeley DB encapsulates all database operations in a structure named DB. The function db_create () is used to create a structure. its prototype is as follows:
Typedef struct _ db DB;
Int db_create (DB ** dbp, DB_ENV * dbenv, u_int32_t flags );
Opening a file stored on the disk as a database is completed by the DB-> open () function. its prototype is as follows:
Int DB-> open (DB * db, DB_TXN * txnid, const char * file,
Const char * database, DBTYPE type, u_int32_t flags, int mode );
The following code demonstrates how to create a DB object handle and how to open a database file:
# Include
# Include
# Include
# Include
# Include
# DefineDATABASE "demo. db"
/* The Program header of the following program code is the same as this */
Int main ()
{DB * dbp;
Int ret;
If (ret = db_create (& dbp, NULL, 0 ))! = 0 ){
Fprintf (stderr, "db_create: % s \ n", db_strerror (ret ));
Exit (1 );
}
If (ret = dbp-> open (dbp, NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664 ))! = 0 ){
Dbp-> err (dbp, ret, "% s", DATABASE );
Exit (1 );
}
}
The code first calls the db_create () function to create a DB object handle. After the call is successful, the variable dbp becomes the database handle, which can be used to configure or access the underlying database. Next, call the DB-> open () function to open the DATABASE file. the "DATABASE" parameter indicates that the corresponding disk file name is demo. db; the parameter "DB_BTREE" indicates that the underlying data structure of the database is B. the parameters "DB_CREATE" and "0664" indicate that a new database file is created when the database file does not exist, set the property value of the file to 0664.
  
Error handling is a routine check required when you open the database. this can be done by calling the DB-> err () function. The "ret" parameter is the error code returned after the Berkeley DB function is called, and other parameters are used to display structured error information.
  
   Add data
To add data to the Berkeley DB database, you can call the DB-> put () function. its prototype is as follows:
Int DB-> put (DB * db, DB_TXN * txnid, DBT * key, DBT * data, u_int32_t flags );
The following code demonstrates how to add new data to a database:
  
Int main ()
{DB * dbp;
DBT key, data;
Int ret;
If (ret = db_create (& dbp, NULL, 0 ))! = 0 ){
Fprintf (stderr, "db_create: % s \ n", db_strerror (ret ));
Exit (1 );
}
If (ret = dbp-> open (dbp,
NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664 ))! = 0 ){
Dbp-> err (dbp, ret, "% s", DATABASE );
Exit (1 );
}
Memset (& key, 0, sizeof (key ));
Memset (& data, 0, sizeof (data ));
Key. data = "sport ";
Key. size = sizeof ("sport ");
Data. data = "football ";
Data. size = sizeof ("football ");
If (ret = dbp-> put (dbp, NULL, & key, & data, 0) = 0)
Printf ("db: % s: key stored. \ n", (char *) key. data );
Else
Dbp-> err (dbp, ret, "DB-> put ");
}
The code first declares two DBT structure variables and fills them with the string "sport" and "football. They are then passed as keywords and data to the DB-> put () function used to add data. The DBT structure is used in almost all functions related to data access.
  
When adding data to a database, if a given keyword already exists, most applications will overwrite existing data. That is to say, if a "sport/basketball" pair has been saved in the database, call the DB-> put () function again to add a "sport/football" pair, the previously stored data will be overwritten. However, Berkeley DB allows you to specify the "DB_NOOVERWRITE" parameter when calling the DB-> put () function and declare that existing data in the database is not overwritten. the code is as follows:
  
If (ret = dbp-> put (dbp, NULL, & key, & data, DB_NOOVERWRITE) = 0)
Printf ("db: % s: key stored. \ n", (char *) key. data );
Else
Dbp-> err (dbp, ret, "DB-> put ");
Once the "DB_NOOVERWRITE" Mark is given, if the DB-> put () function finds that the given keyword already exists in the database during execution, the Key/Data pair cannot be successfully added to the database, so the error code "DB_KEYEXIST" is returned ".
  
   Retrieve data
You can call the DB-> get () function to retrieve data from the Berkeley DB database. its prototype is as follows:
  
Int DB-> get (DB * db, DB_TXN * txnid, DBT * key, DBT * data, u_int32_t flags );
The following code demonstrates how to retrieve the required data from the database:
Int main ()
{DB * dbp;
DBT key, data;
Int ret;
If (ret = db_create (& dbp, NULL, 0 ))! = 0 ){
Fprintf (stderr, "db_create: % s \ n", db_strerror (ret ));
Exit (1 );
}
If (ret = dbp-> open (dbp,
NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664 ))! = 0 ){
Dbp-> err (dbp, ret, "% s", DATABASE );
Exit (1 );
}
Memset (& key, 0, sizeof (key ));
Memset (& data, 0, sizeof (data ));
Key. data = "sport ";
Key. size = sizeof ("sport ");
If (ret = dbp-> get (dbp, NULL, & key, & data, 0) = 0)
Printf ("db: % s: key retrieved: data was % s. \ n ",
(Char *) key. data, (char *) data. data );
Else
Dbp-> err (dbp, ret, "DB-> get ");
}
The code also declares two DBT structure variables and calls the memset () function to clear their content. Although Berkeley DB does not require data to be cleared before data operations are performed, we recommend that you clear the data before improving the code quality. When performing data retrieval, it is essential to process the return values of the DB-> get () function, because it carries information such as whether the retrieval operation is successful or not. The returned values of the DB-> get () function are listed below:
  
◆ The 0 function is successfully called and the specified keyword is found;
  
◆ The DB_NOTFOUND function is successfully called, but the specified keyword is not found;
  
◆ Failed to call a function greater than 0. a system error may occur.
  
   Delete data
You can call the DB-> del () function to delete data from the Berkeley DB database. its prototype is as follows:
  
Int DB-> del (DB * db, DB_TXN * txnid, DBT * key, u_int32_t flags );
The following code deletes data from a database:
  
Int main ()
{DB * dbp;
DBT key;
Int ret;
If (ret = db_create (& dbp, NULL, 0 ))! = 0 ){
Fprintf (stderr, "db_create: % s \ n", db_strerror (ret ));
Exit (1 );
}
If (ret = dbp-> open (dbp,
NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664 ))! = 0 ){
Dbp-> err (dbp, ret, "% s", DATABASE );
Exit (1 );
}
Memset (& key, 0, sizeof (key ));
Key. data = "sport ";
Key. size = sizeof ("sport ");
If (ret = dbp-> del (dbp, NULL, & key, 0) = 0)
Printf ("db: % s: key was deleted. \ n", (char *) key. data );
Else
Dbp-> err (dbp, ret, "DB-> del ");
}
To delete data, you only need to provide the corresponding keywords, and do not need to specify the corresponding data.
  
   Close database
Shutting down a database is an indispensable part of a complete database operation process. This is because Berkeley DB depends on the system's underlying buffer mechanism. that is to say, only when the database is shut down normally can all the modified data be written to the disk, at the same time, the resources it occupies can be truly released. The database closure operation is completed by calling the DB-> close () function. its prototype is as follows:
  
Int DB-> close (DB * db, u_int32_t flags );
The following code demonstrates how to close the database as needed:
  
Int main ()
{DB * dbp;
DBT key, data;
Int ret, t_ret;
If (ret = db_create (& dbp, NULL, 0 ))! = 0 ){
Fprintf (stderr, "db_create: % s \ n", db_strerror (ret ));
Exit (1 );
}
If (ret = dbp-> open (dbp,
NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664 ))! = 0 ){
Dbp-> err (dbp, ret, "% s", DATABASE );
Goto err;
}
Memset (& key, 0, sizeof (key ));
Memset (& data, 0, sizeof (data ));
Key. data = "sport ";
Key. size = sizeof ("sport ");
If (ret = dbp-> get (dbp, NULL, & key, & data, 0) = 0)
Printf ("db: % s: key retrieved: data was % s. \ n ",
(Char *) key. data, (char *) data. data );
Else
Dbp-> err (dbp, ret, "DB-> get ");
If (t_ret = dbp-> close (dbp, 0 ))! = 0 & ret = 0)
Ret = t_ret;
Exit (ret );
}
   Summary
The embedded database system Berkeley DB is easy to use. It does not have the concept of a database server and does not require complex SQL statements. all operations and management on data can be performed.

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.