#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <db.h>
#define DATABASE "geo.db"
int main(int argv,char **argc){
DBT key,data;
DBC *dbcp;
DB *dbp;
int ret;
if((ret=db_create(&dbp,NULL,0))!=0){
fprintf(stderr,"db_create: %s\n",db_strerror(ret));
exit(1);
}
dbp->set_flags(dbp,DB_DUP); // we set that so that we can store multiple keys!
if((ret = dbp->open(dbp,NULL,DATABASE,NULL,DB_BTREE,DB_CREATE,0644))!=0){
dbp->err(dbp,ret,"%s",DATABASE);
}
memset(&key,0,sizeof(key));
memset(&data,0,sizeof(data));
/*
key.data="AT";
key.size=sizeof("AT");
data.data="VIE";
data.size=sizeof("VIE");
*/
// switch(ret=dbp->put(dbp,NULL,&key,&data,DB_NOOVERWRITE)){
/*switch(ret=dbp->put(dbp,NULL,&key,&data,0)){
case 0: printf("db: %s: key stored.\n",(char*)key.data);
break;
case DB_KEYEXIST:
printf("db: %s: key previously stored.\n",(char*)key.data);
break;
default:
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);
} else {
dbp->err(dbp,ret,"DB->get");
}*/
if((ret=dbp->cursor(dbp,NULL,&dbcp,0))!=0){
dbp->err(dbp,ret,"DB->cursor");
return(1);
}
while((ret=dbcp->c_get(dbcp,&key,&data,DB_NEXT))==0){
printf("%s\n",(char*)data.data);
/* if(ret!=DB_NOTFOUND){
dbp->err(dbp,ret,"DBcursor->get");
} */
}
dbcp->c_close(dbcp);
dbp->close(dbp,0);
return(0);
} |