分类: LINUX
2008-06-16 21:14:17
Author: misty(qibing83@yahoo.com.cn)
1.1 下载及安装
1.2 实现及验证
#gcc –I/home/qibing/opt/inclue –L/home/qibing/opt/lib –o Berkeley Berkeley.c –ldb-4.4
#include
//only this head should include for use bdb.
#define DATABASE "demo.db"
//........... Create an environment object and initialize it for error reporting
fprintf(stderr, "Error creating env handle: %s\n", db_strerror(ret));
//........If the environment does not exist create it. Initialize the in-memory cache.
ret = myEnv->open(myEnv,"/home/yangbin1/yangjian/my/db/testevn",env_flags,0);
fprintf(stderr, "Environment open failed: %s", db_strerror(ret));
if ((ret = db_create(&dbp, myEnv, 0)) != 0)
fprintf(stderr, "db_create: %s\n", db_strerror(ret));
if ((ret = dbp->open(dbp, NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0)
dbp->err(dbp, ret, "%s", DATABASE);
memset(&data, 0, sizeof(data)); key.data = "sport";
if ((ret = dbp->put(dbp, NULL, &key, &data, 0)) == 0)
printf("db: %s: key stored.\n", (char *)key.data);
dbp->err(dbp, ret, "DB->put");
if ((ret = dbp->put(dbp, NULL, &key, &data, DB_NOOVERWRITE)) == 0)
printf("db: %s: key stored.\n", (char *)key.data);
else dbp->err(dbp, ret, "DB->put");
if ((ret = dbp->get(dbp, NULL, &key, &data, 0)) == 0)
printf("db: %s: key retrieved: data was %s.\n", (char *)key.data, (char *)data.data);
dbp->err(dbp, ret, "DB->get");
printf("db: %s: key was deleted.\n", (char *)key.data);
//.........close, only when the db successful closed,the data can real write to the disk.
//if ((t_ret = dbp->close(dbp, 0)) != 0 && ret == 0)
//........When you are done with an environment, you must close it.
//........Before you close an environment, make sure you close any opened databases
|
参考:http://www.cnblogs.com/huqingyu/archive/2006/10/06/522251.html