很重要的数据库, 高效! 必须要学习的.
入门知识:
http://www.cnblogs.com/huqingyu/archive/2006/10/06/522251.html------------
使用Berkeley DB (BDB)必须理解的几个基本概念: 1>. Key/data pairs
BDB使用key和data成对出现来表示数据库中的一行记录.
如:
Key: Data:
---------------
fruit apple
sport cricket
drink water
Key and data items can be arbitrary binary data of practically any length, including 0 bytes. There is a single data item for each key item, by default, but databases can be configured to support multiple data items for each key item.
2>. Object handles
整个数据库使用起来采用面向对象的思想.
3>. Error returns
BDB API通常的错误返回:
0 执行成功
非0 执行失败
如果是系统错误, 如: 磁盘满, 没有操作权限, 非法的参数设置, BDB将返回errno值. 当然errno是大于0的.
db_strerror API返回任何错误消息, 类似ANSI C中的strerror API一样.
还有两个API, DB->err, DB->errx, 类似ANSI C中的printf
数据库使用需要理解的: 1>. 创建一个简单的数据库
打开一个数据库需要两步:
setp 1. 调用db_create API创建DB句柄
setp 2. 使用句柄DB->open来打开数据库.
如:
#include
#include
#include
#define DATABASE "access.db"
int main()
{
DB *dbp;
int ret;
if ((ret = db_create(&dbp, NULL, 0)) != 0)
{
fprintf(stderr, "db_create: %s\n", db_strerror(ret));
exit (1);
}
if ((ret = dbp->open(dbp, NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0)
{
dbp->err(dbp, ret, "%s", DATABASE);
goto err;
}
....
}
The db_create interface takes three arguments:
dbp
A location to store a reference to the created structure.
environment
A location to specify an enclosing Berkeley DB environment, not used in our example.
flags
A placeholder for flags, not used in our example.
The DB->open interface takes five arguments:
file
The name of the database file to be opened.
database
The optional database name, not used in this example.
type
The type of database to open. This value will be one of the four access methods Berkeley DB supports: DB_BTREE, DB_HASH, DB_QUEUE or DB_RECNO, or the special value DB_UNKNOWN, which allows you to open an existing file without knowing its type.
flags
Various flags that modify the behavior of DB->open. In our simple case, the only interesting flag is DB_CREATE. This flag behaves similarly to the IEEE/ANSI Std 1003.1 (POSIX) O_CREATE flag to the open system call, causing Berkeley DB to create the underlying database if it does not yet exist.
mode
The file mode of any underlying files that DB->open will create. The mode behaves as does the IEEE/ANSI Std 1003.1 (POSIX) mode argument to the open system call, and specifies file read, write and execute permissions. Of course, only the read and write permissions are relevant to Berkeley DB.
2>. 向数据库中添加数据
3>. 从数据库中查找记录
4>. 从数据库中删除记录
5>. 关闭数据库
See More:
阅读(2541) | 评论(0) | 转发(0) |