Basic use of Berkeley DB
After downloading and installing the database, you can use this database as an example.
1. Create a database
# Include <db_cxx.h>
# Include <iostream>
# Include <string>
Using namespace STD;
Int main (INT, char * [])
{
Try
{
DB dB (null, null );
//
// D: // mytest. DB data file directory
// Structure of the file stored in db_btree
// Open in db_create Mode
// Null does not use this structure in Windows
//
If (0 = dB. Open (null, "d: // mytest. DB", "myfirstdb", db_btree, db_create, null ))
{
Cout <"Create a database file! "<Endl;
}
}
Catch (dbexception & E)
{
Cout <"failed to create database :";
Cout <E. What () <Endl;
}
Return 0;
}
2. Save a record to the database
# Include <iostream>
# Include <db_cxx.h>
Using namespace STD;
Typedef unsigned int uint32, * puint32;
Typedef unsigned char uint8, * puint8;
# Pragma pack (push, 1)
Typedef struct _ smyuser
{
Uint32 userid;
Char name [32];
} Smyuser;
# Pragma pack (POP)
Void main ()
{
Try
{
DB dB (null, null );
If (db. Open (null, "d: // mytest. DB", "myfirstdb", db_btree, db_rdonly, null) = 0)
{
Smyuser resinst;
Memset (& resinst, 0, sizeof (smyuser ));
Resinst. userid = 1;
DBT dbkey (& (resinst. userid), sizeof (resinst. userid ));
DBT dbdata;
Memset (& dbdata, 0, sizeof (dbdata ));
// DBT dbdata (& resinst, sizeof (resinst ));
If (0 = dB. Get (null, & dbkey, & dbdata, 0 ))
{
Cout <"added successfully" <Endl;
Smyuser * presinst = (smyuser *) dbdata. get_data ();
Cout <"content:" <(char *) presinst-> name <Endl;
}
}
}
Catch (dbexception & E)
{
Cout <"failed:" <E. What () <Endl;
}
Cout <"Hello the world" <Endl;
}
3. retrieve data from the database
# Include <iostream>
# Include <db_cxx.h>
Using namespace STD;
Typedef unsigned int uint32, * puint32;
Typedef unsigned char uint8, * puint8;
# Pragma pack (push, 1)
Typedef struct _ smyuser
{
Uint32 userid;
Char name [32];
} Smyuser;
# Pragma pack (POP)
Void main ()
{
Try
{
DB dB (null, null );
If (db. Open (null, "d: // mytest. DB", "myfirstdb", db_btree, db_rdonly, null) = 0)
{
Smyuser resinst;
Memset (& resinst, 0, sizeof (smyuser ));
Resinst. userid = 1;
DBT dbkey (& (resinst. userid), sizeof (resinst. userid ));
DBT dbdata;
Memset (& dbdata, 0, sizeof (dbdata ));
// DBT dbdata (& resinst, sizeof (resinst ));
If (0 = dB. Get (null, & dbkey, & dbdata, 0 ))
{
Cout <"added successfully" <Endl;
Smyuser * presinst = (smyuser *) dbdata. get_data ();
Cout <"content:" <(char *) presinst-> name <Endl;
}
}
}
Catch (dbexception & E)
{
Cout <"failed:" <E. What () <Endl;
}
Cout <"Hello the world" <Endl;
}
4. delete a record
# Include <iostream>
# Include <db_cxx.h>
Using namespace STD;
Typedef unsigned int uint32, * puint32;
Typedef unsigned char uint8, * puint8;
# Pragma pack (push, 1)
Typedef struct _ smyuser
{
Uint32 userid;
Char name [32];
} Smyuser;
# Pragma pack (POP)
Int main ()
{
Try
{
DB dB (null, null );
If (db. Open (null, "d: // mytest. DB", "myfirstdb", db_btree, null, null) = 0)
{
Smyuser resinst;
Memset (& resinst, 0, sizeof (smyuser ));
Resinst. userid = 1;
DBT dbkey (& (resinst. userid), sizeof (resinst. userid ));
If (0 = dB. Del (null, & dbkey, 0 ))
{
Cout <"added successfully" <Endl;
}
}
}
Catch (dbexception & E)
{
Cout <"failed:" <E. What () <Endl;
}
Return 0;
}
These are some basic operations. I want to explain that this Berkeley dB emphasizes the concept of key and data. They are all very primitive buffer blocks. Based on the foundation of this database, we can do some work on our own. In fact, we can implement a database on our own. The above myfirstdb is like a table.