?? 新建 文本文檔 (2).txt
字號(hào):
下面是主函數(shù):
// 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;
}
bool loadPeople(char *fileName, uint32 index, DBSerialize &obj)
{
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
}
Dbt key, data;
key.set_data(&index);
key.set_size(sizeof(index));
if (db.get(NULL, &key, &data, 0) != 0) {
db.close(0);
return false;
}
DBInStream in(data.get_data(), data.get_size());
obj.load(in);
db.close(0);
return true;
}
bool loadPeople(char *fileName, string index, DBSerialize &obj)
{
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
}
Dbt key, data;
key.set_data((void*)index.c_str());
key.set_size(index.length()+1);
if (db.get(NULL, &key, &data, 0) != 0) {
db.close(0);
return false;
}
DBInStream in(data.get_data(), data.get_size());
obj.load(in);
db.close(0);
return true;
}
bool savePeople(char *fileName, uint32 index, DBSerialize &obj)
{
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
}
DBOutStream out;
obj.save(out);
Dbt key, data;
key.set_data(&index);
key.set_size(sizeof(index));
data.set_data(out.getData());
data.set_size(out.getSize());
int ret = db.put(NULL, &key, &data, 0) ;
db.close(0);
return (ret == 0);
}
bool savePeople(char *fileName, string index, DBSerialize &obj)
{
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
}
DBOutStream out;
obj.save(out);
Dbt key, data;
key.set_data((void*)index.c_str());
key.set_size(index.length()+1);
data.set_data(out.getData());
data.set_size(out.getSize());
int ret = db.put(NULL, &key, &data, 0);
db.close(0);
return (ret == 0);
}
bool printAllPeople(char *fileName)
{
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;
// Iterate over the database, deleting each record in turn.
int ret;
while ((ret = cursorp->get(&key, &data,
DB_NEXT)) == 0) {
//cursorp->del(0);
CPerson p;
DBInStream in(data.get_data(), data.get_size());
p.load(in);
printf("%s\n",p.Describe);
}
} 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;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
string index1 = "Liang Zhijian";
string index2 = "Zou Dan";
CPerson p1;
p1.age = 0;
p1.Name = "Liang Zhijian";
p1.Describe = "student1";
savePeople("mydb.db",index1,p1);
CPerson p2;
p2.age = 1;
p2.Name = "Zou Dan";
p2.Describe = "student2";
savePeople("mydb.db",index2,p2);
CPerson p3;
loadPeople("mydb.db","Liang Zhijian",p3);
printf("%s\n",p3.Describe);
printAllPeople("mydb.db");
delPeople("mydb.db","Liang Zhijian");
printAllPeople("mydb.db");
}
return nRetCode;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -