Basic use of Berkeley DB

Source: Internet
Author: User

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.


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.