在下载安装这个数据库之后,来使用这样的数据库做一个例子。
1.创建一个数据库
#include
<
db_cxx.h
>
#include
<
iostream
>
#include
<
string
>
using
namespace
std;
int
main(
int
,
char
*
[])
{
try
{
Db db(NULL,NULL);
//
//
d:\\MyTest.db 数据文件目录
//
DB_BTREE 使用的文件保存的结构
//
DB_CREATE 创建方式的打开
//
NULL 在windows里不使用此结构
//
if
(
0
==
db.open(NULL,
"
d:\\MyTest.db
"
,
"
MyFirstDB
"
,DB_BTREE,DB_CREATE,NULL))
{
cout
<<
"
创建数据库文件!
"
<<
endl;
}
}
catch
(DbException
&
e)
{
cout
<<
"
创建数据库失败:
"
;
cout
<<
e.what()
<<
endl;
}
return
0
;
}
2.向数据库里保存一个记录
#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
<<
"
增加成功
"
<<
endl;
SMyUser
*
pResInst
=
(SMyUser
*
)dbData.get_data();
cout
<<
"
内容:
"
<<
(
char
*
)pResInst
->
name
<<
endl;
}
}
}
catch
(DbException
&
e)
{
cout
<<
"
失败:
"
<<
e.what()
<<
endl;
}
cout
<<
"
hello the world
"
<<
endl;
}
3.从数据库里检索出数据
#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
<<
"
增加成功
"
<<
endl;
SMyUser
*
pResInst
=
(SMyUser
*
)dbData.get_data();
cout
<<
"
内容:
"
<<
(
char
*
)pResInst
->
name
<<
endl;
}
}
}
catch
(DbException
&
e)
{
cout
<<
"
失败:
"
<<
e.what()
<<
endl;
}
cout
<<
"
hello the world
"
<<
endl;
}
4.删除一条记录
#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
<<
"
增加成功
"
<<
endl;
}
}
}
catch
(DbException
&
e)
{
cout
<<
"
失败:
"
<<
e.what()
<<
endl;
}
return
0
;
}
这一些东西都是一些基本的操作。主要是想说明这个Berkeley DB是很强调一个这样的概念:key与Data。他们都是一个很原始buffer块。基于这一个数据库的基础,我们可自己做一些工作,其实有一些像自己去实现一数据库出来。这个上面的MyFirstDB这个东东,感觉上真的是像一个表。
[Sword.Hell]
posted on 2006-12-14 11:14
Sword.Hell blog 阅读(1563)
评论(1) 编辑 收藏 引用 所属分类:
使用Berkeley DB