bdb专题
一、示例程序#include #include #include int main(){ DB *db; DBT key, data; DBC *cursorp; int ret; char tmpstr[4]; /* init key, data,seems meanless */ memset(&key, 0, sizeof(DBT)); mem
一、下载(4.6.21版本)
http://www.oracle.com/technetwork/database/berkeleydb/downloads/index-082944.html
二、编译与安装
2.1 解压db-4.6.21.tar.gz;
2.2 cd db-4.6.21/build_unix
2.3 生成Makefile文件: ../configure
2.4 生成程序: make
2.5 安装程序:make install
2.6 安装后的目录: /usr/local/BerkeleyDB.4.6
三、示例程序
#include <stdio.h>
#include <string.h>
#include <db.h>
int main()
{
DB *db;
DBT key, data;
DBC *cursorp;
int ret;
char tmpstr[4];
/* init key, data,seems meanless */
memset(&key, 0, sizeof(DBT));
memset(&data, 0, sizeof(DBT));
/* create a db handle */
db_create(&db, NULL, 0);
/* open db mydb.db, create it if not exist */
db->open(db, NULL, "mydb.db", NULL, DB_BTREE, DB_CREATE, 0);
/* put key and data pair into bdb.db */
int i = 0;
for(i = 0; i < 10; i ++)
{
/* set value of key and data pair to be insert into mydb.db */
sprintf(tmpstr, "key%d", i);
key.data = tmpstr;
key.size = strlen(key.data);
data.data = tmpstr;
data.size = strlen(data.data);
ret = db->put(db, NULL, &key, &data, DB_NOOVERWRITE);
printf("ret:%d/n", ret); /* ret == 0 means inserting success */
}
/* get data from db */
data.data = "i am key11111's data";
strcpy(tmpstr, "key0");
key.data = &tmpstr;
key.ulen = sizeof(key.data);
key.flags = DB_DBT_USERMEM;
if(0 != db->get(db, NULL, &key, &data, 0))
{
printf("data not found!/n"); /*why?*/
}
else
{
printf("key's data:%s/n", data.data);
}
/* close db handle */
if(NULL != db)
{
db->close(db, 0);
}
return 0;
}
更多推荐
所有评论(0)