Ok... thank you.
and can I ask another question?
dbtest.c is so source that one data put into key. so I wonder that is possible to store severl data under one key. for example:
(key) : (data)
fruit = apple,orange,banada
vegetalbe = carot,onion,
...
....
and above source is just for storing data.Therefore if I want to make a program in order to just read berkeley db data, how can I make it ? I made following these sources but it doesn't work. That is,i want to make two program Data is stored by input.c and data is read by reading.c
== input.c ===
#include <stdio.h>
#include <sys/types.h>
#include <stdio.h>
#include <db.h>
#define DATABASE "access.db"
#define SSIZE 256
typedef struct _content{
char RE[SSIZE];
char REURL[SSIZE];
char ACCEL[SSIZE];
} CONTENT;
CONTENT content;
static char* _cpBase64Encoding =
"ABCDEFGHIJKLMNOPQRSTUVWXY
void Base64Encode( char* cpInput, char* cpOutput )
{
int nIdx[ 4 ];
while ( '\0' != *cpInput )
{
nIdx[0] = ((*cpInput) & 0xFC)>>2;
nIdx[1] = ((*cpInput) & 0x03)<<4;
cpInput++;
if ( '\0' != *cpInput )
{
nIdx[1] |= ((*cpInput) & 0xF0)>>4;
nIdx[2] = ((*cpInput) & 0x0F)<<2;
cpInput++;
if ( '\0' != (*cpInput) )
{
nIdx[2] |= ((*cpInput) & 0xC0) >> 6;
nIdx[3] = (*cpInput) & 0x3F;
cpInput++;
}
else
nIdx[3] = 64;
}
else
{
nIdx[2] = 64;
nIdx[3] = 64;
}
*(cpOutput+0) = *(_cpBase64Encoding + nIdx[0]);
*(cpOutput+1) = *(_cpBase64Encoding + nIdx[1]);
*(cpOutput+2) = *(_cpBase64Encoding + nIdx[2]);
*(cpOutput+3) = *(_cpBase64Encoding + nIdx[3]);
cpOutput += 4;
}
*cpOutput = '\0';
return;
}
int main (){
FILE *fp;
DB *dbp;
DBT key,data;
int ret,t_ret;
int size=600;
char buf[size];
char sNonCoded[256];
char ssNonCoded[256];
char sssNonCoded[256];
char ssssNonCoded[256];
char sEncoded[256];
char ssEncoded[256];
char sssEncoded[256];
char ssssEncoded[256];
char regex[6];
char redirect[512];
char redirect_url[512];
char accel[512];
//Read data
if((fp=fopen("2.txt","rt")
printf("Error: Cannot open data file\n");
return(1);
}
// Create Berkeley DB
if ((ret = db_create(&dbp, NULL, 0)) != 0) {
fprintf(stderr, "db_create: %s\n", db_strerror(ret));
exit (1);
}
if ((ret = dbp->open(
dbp, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) {
dbp->err(dbp, ret, "%s", DATABASE);
goto err;
}
while(fgets(buf,size-1,fp)
sscanf(buf,"%6s %512s %512s %512s",regex,redirect,redi
strcpy(sNonCoded,regex);
strcpy(ssNonCoded,redirect
strcpy(sssNonCoded,redirec
strcpy(ssssNonCoded,accel)
Base64Encode( sNonCoded, sEncoded );
Base64Encode( ssNonCoded, ssEncoded );
Base64Encode( sssNonCoded, sssEncoded );
Base64Encode( ssssNonCoded, ssssEncoded);
memset(&key,0,sizeof(key))
memset(&data,0,sizeof(data
key.data = sEncoded;
key.size = sizeof(sEncoded);
strncpy(content.RE,ssEncod
strncpy(content.REURL,sssE
strncpy(content.ACCEL,ssss
data.data = (char *)&content;
data.size = sizeof(CONTENT);
if ((ret = dbp->put(dbp,NULL,&key,&da
printf("db: %s: field 1 stored.\n",(char *)key.data);
printf("db: %s: field 2 stored.\n",content.RE);
printf("db: %s: field 3 stored.\n",content.REURL);
printf("db: %s: field 4 stored.\n",content.ACCEL);
}else{
printf("Error");
dbp->err(dbp,ret,"DB->put"
goto err;
}
}
err: if ((t_ret = dbp->close(dbp, 0)) != 0 && ret == 0)
ret = t_ret;
exit(ret);
}
== reading.c =====
#include <stdio.h>
#include <sys/types.h>
#include <stdio.h>
#include <db.h>
#define DATABASE "access.db"
#define SSIZE 256
typedef struct _content{
char RE[SSIZE];
char REURL[SSIZE];
char ACCEL[SSIZE];
} CONTENT;
CONTENT content;
int main (){
FILE *fp;
DB *dbp;
DBT key,data;
int ret,t_ret,i=0;
char sNonCoded[256];
char rkey[1000 + 1], *retaddr;
// Create Berkeley DB
if ((ret = db_create(&dbp, NULL, 0)) != 0) {
fprintf(stderr, "db_create: %s\n", db_strerror(ret));
exit (1);
}
if ((ret = dbp->open(
dbp, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) {
dbp->err(dbp, ret, "%s", DATABASE);
goto err;
}
while((ret = dbp->get(dbp, NULL, &key, &data, 0)) != 0){
printf("%d\n",i);
memset(&key,0,sizeof(key))
memset(&data,0,sizeof(data
key.data = rkey;
key.size = sizeof(rkey);
data.data = (char *)&content;
data.size = sizeof(CONTENT);
printf( "Decoded(field 1): %s\n", (char *)key.data );
printf( "Decoded(field 2): %s\n",((CONTENT *)(data.data))->RE);
printf( "Decoded(field 2): %s\n", ((CONTENT *)(data.data))->REURL);
printf( "Decoded(field 2): %s\n\n", ((CONTENT *)(data.data))->ACCEL);
i++;
if ( i == 20) {
exit(1);
}
}
err: if ((t_ret = dbp->close(dbp, 0)) != 0 && ret == 0)
ret = t_ret;
exit(ret);
}
I apprecate of your help..!!
Main Topics
Browse All Topics





by: catbuttPosted on 2003-02-09 at 19:47:59ID: 7915349
Make sure db.h is in your include path and that you link to library db ("ldb").
I use a makefile rather than just invloing gcc directly, here is part of my makefile has in it:
#--------
BDB_DIR = /usr/local/BerkeleyDB.4.0
CFLAGS = -Wall -g \
-Iinclude \
-I$(BDB_DIR)/include
LDFLAGS = -Wall -g -L$(BDB_DIR)/lib
LIBS = -ldb -lpthread -lnsl -lresolv -lz
#--------
Notice the -ldb and the way I put bdb's installed directory in the include path.