why n="基于berkeley db的个人资料管理系统"? -
04-23-2006
, 04:07 AM
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//only this head should include for use bdb.
#include "db_cxx.h"
#include "db.h"
#define N 200
typedef struct wendang{
char* name;
char* direct;
char* miaoshu;
}WEN;
WEN wen[3];
class BDB
{
public:
Db db;
private:
u_int32_t oFlags;
void close();
Dbt keyn;
Dbt keyd;
Dbt keym;
Dbt datan,datad,datam;
public:
BDB();
// Our destructor just calls our private close method.
~BDB() { close(); }
//读出
char* get();
//修改//修改的时候先删除,再写入操作
xiugai(WEN xindata );
};
//implementation for the constructor
BDB :: BDB()
: db(NULL, 0), // Instantiate Db object
oFlags(DB_CREATE),
keyn("keywordn", strlen("keywordn") + 1),
keyd("keywordd", strlen("keywordd") + 1),
keym("keywordm", strlen("keywordm") + 1)
{
try
{
db.open(NULL, // Transaction pointer
"wendang.db", // Database file name
NULL, // Optional logical database name
DB_BTREE, // Database access method
oFlags, // Open flags
0); // File mode (using defaults)
}
catch(DbException &e)
{
// Error handling code goes here
std::cerr << e.what() << std::endl;
}
catch(std::exception &e)
{
// Error handling code goes here
std::cerr << e.what() << std::endl;
}
}
void BDB::close()
{
// Close the db
try
{
db.close(0);
// DbException is not subclassed from std::exception, so
// need to catch both of these.
}
catch(DbException &e)
{
std::cerr << e.what() << std::endl;
}
catch(std::exception &e)
{
std::cerr << e.what() << std::endl;
}
}
char* BDB::get()
{
WEN x;
int rett=db.get(NULL, &keyn, &datan, 0);
char *n=(char*)datan.get_data();
x.name=n;
db.get(NULL, &keyd, &datad, 0);
char *d=(char*)datad.get_data();
x.direct=d;
db.get(NULL, &keym, &datam, 0);
char *m=(char*)datam.get_data();
x.miaoshu=m;
return n;
}
BDB::xiugai(WEN xindata)
{
int rettt=db.del(NULL, &keyn, 0);
db.del(NULL, &keyd, 0);
db.del(NULL, &keym, 0);
Dbt datan(xindata.name, strlen(xindata.name)+1);
Dbt datad(xindata.direct, strlen(xindata.direct)+1);
Dbt datam(xindata.miaoshu, strlen(xindata.miaoshu)+1);
// data.set_data(xindata);
// data.set_ulen(sizeof(xindata));
// data.set_flags(DB_DBT_USERMEM);
int ret= db.put(NULL, &keyn, &datan, DB_NOOVERWRITE);
db.put(NULL, &keyd, &datad, DB_NOOVERWRITE);
db.put(NULL, &keym, &datam, DB_NOOVERWRITE);
}
void main()
{
BDB ceshi;
wen[0].name="伯克力数据库";
wen[0].direct="c:\\bdb\\lwu1";
wen[0].miaoshu="基于berkeley db的个人资料管理系统";
// ceshi.xiugai(wen[0]);
// WEN cs0=ceshi.get();
char* x0=ceshi.get();
// char *x0=cs0.name;
printf("%s\n",x0);
// char *y0=cs0.direct;
// char *z0=cs0.miaoshu;
// printf("%s %s %s\n",x0,y0,z0);
} |