下面是主函数:
// TestBerkeleyDB.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "TestBerkeleyDB.h"
#include "db_cxx.h"
#include "icqtypes.h"
#include "icqdb.h"
#include "Person.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
//using namespace std;
bool delPeople(char *fileName, string index)
{
Db db(NULL, 0); // Instantiate the Db object
u_int32_t oFlags = DB_CREATE; // Open flags;
try {
// Open the database
db.open(NULL, // Transaction pointer
fileName, // Database file name
NULL, // Optional logical database name
DB_BTREE, // Database access method
oFlags, // Open flags
0); // File mode (using defaults)
// DbException is not subclassed from std::exception, so
// need to catch both of these.
} catch(DbException &e) {
// Error handling code goes here
} catch(std::exception &e) {
// Error handling code goes here
}
Dbc *cursorp;
try {
// Database open omitted
// Get the cursor
db.cursor(NULL, &cursorp, 0);
// Set up our DBTs
Dbt data;
Dbt key;
key.set_data((void*)index.c_str());
key.set_size(index.length()+1);
// Iterate over the database, deleting each record in turn.
int ret;
while ((ret = cursorp->get(&key, &data,
DB_SET)) == 0) {
cursorp->del(0);
}
} catch(DbException &e) {
db.err(e.get_errno(), "Error!");
} catch(std::exception &e) {
db.errx("Error! %s", e.what());
}
// Cursors must be closed
if (cursorp != NULL)
cursorp->close();
db.close(0);
return true;
}
bool delPeople(char *fileName, uint32 index)
{
Db db(NULL, 0); // Instantiate the Db object
u_int32_t oFlags = DB_CREATE; // Open flags;
try {
// Open the database
db.open(NULL, // Transaction pointer
fileName, // Database file name
NULL, // Optional logical database name
DB_BTREE, // Database access method
oFlags, // Open flags
0); // File mode (using defaults)
// DbException is not subclassed from std::exception, so
// need to catch both of these.
} catch(DbException &e) {
// Error handling code goes here
} catch(std::exception &e) {
// Error handling code goes here
}
Dbc *cursorp;
try {
// Database open omitted
// Get the cursor
db.cursor(NULL, &cursorp, 0);
// Set up our DBTs
Dbt data;
Dbt key;
key.set_data(&index);
key.set_size(sizeof(index));
// Iterate over the database, deleting each record in turn.
int ret;
while ((ret = cursorp->get(&key, &data,
DB_SET)) == 0) {
cursorp->del(0);
}
} catch(DbException &e) {
db.err(e.get_errno(), "Error!");
} catch(std::exception &e) {
db.errx("Error! %s", e.what());
}
// Cursors must be closed
if (cursorp != NULL)
cursorp->close();
db.close(0);
return true;
}
