Chinaunix首页 | 论坛 | 博客
  • 博客访问: 191548
  • 博文数量: 17
  • 博客积分: 2641
  • 博客等级: 少校
  • 技术积分: 271
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-28 20:11
文章分类

全部博文(17)

文章存档

2014年(2)

2012年(2)

2011年(4)

2010年(1)

2009年(1)

2008年(7)

分类: LINUX

2008-06-16 21:14:17

在嵌入式Linux上实现BerkeleyDB数据库

Author: misty(qibing83@yahoo.com.cn)

Berkeley DB原是Sleepycat软件公司的产品,现被Oracle收购。Berkeley DB使用简单的接口来完成所有数据库的操作,而不是使用SQL语言。

1.1        下载及安装

到Oracle网站上下载版本db-4.4.20.NC.tar.gz,解压后,到目录build-unix下,运行
#../dist/configure –prefix=/home/qibing/opt
#make
#make install
则库文件和头文件安装在目录/home/qibing/opt下,要完全删除运行 #make realclean即可。

1.2        实现及验证

建立文件berkeley.c,并使用下面命令编译

#gcc –I/home/qibing/opt/inclue –L/home/qibing/opt/lib –o Berkeley Berkeley.c –ldb-4.4

 
Berkeley.c

#include

#include
#include
#include

//only this head should include for use bdb.

#include  

#define DATABASE "demo.db"

 
int main()
{
    DB_ENV *myEnv;
    DB *dbp;
    DBT key, data;
    int ret,t_ret;
    u_int32_t env_flags;

    //........... Create an environment object and initialize it for error reporting

    ret = db_env_create(&myEnv, 0);
    if (ret != 0)
    {

          fprintf(stderr, "Error creating env handle: %s\n", db_strerror(ret));

          return -1;
    }

    //........If the environment does not exist create it. Initialize the in-memory cache.

    env_flags = DB_CREATE | DB_INIT_MPOOL;
    //........Open the environment.

    ret = myEnv->open(myEnv,"/home/yangbin1/yangjian/my/db/testevn",env_flags,0);

    if (ret != 0)
    {

          fprintf(stderr, "Environment open failed: %s", db_strerror(ret));

          return -1;
    }
 

    if ((ret = db_create(&dbp, myEnv, 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);

          exit (1);
    }
    memset(&key, 0, sizeof(key));

    memset(&data, 0, sizeof(data)); key.data = "sport";

    key.size = sizeof("sport");
    data.data = "football";
    data.size = sizeof("football");
/*
    //......put data

    if ((ret = dbp->put(dbp, NULL, &key, &data, 0)) == 0)

    {

          printf("db: %s: key stored.\n", (char *)key.data);

    }
      else
    {

          dbp->err(dbp, ret, "DB->put");

    }
*/
 
    //........put data NOOVERWRITE

    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");

 
    //.......get data

    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);

    else

    dbp->err(dbp, ret, "DB->get");

 
    //......del data
    if((ret = dbp->del(dbp, NULL, &key, 0)) == 0)

    printf("db: %s: key was deleted.\n", (char *)key.data);

    else
    dbp->err(dbp, ret, "DB->del");
 

    //.........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)

    //ret = t_ret;
    //exit(ret);
    if (dbp != NULL)
    dbp->close(dbp, 0);
    //.........close evn

    //........When you are done with an environment, you must close it.

    //........Before you close an environment, make sure you close any opened databases

    if (myEnv != NULL)
    myEnv->close(myEnv, 0);
 
    return 0;
}
End of Berkeley.c
编译好后,运行./berkeley即可。 附件为本文中的代码(这个代码我未具体验证)


文件:berkeley.rar
大小:1KB
下载:下载

参考:http://www.cnblogs.com/huqingyu/archive/2006/10/06/522251.html

在嵌入式上实现时,用交叉编译gcc编译并把库放在目标板上。
阅读(2061) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~