Retreving the dynamically allocated values from bdb using C
In one of the assignment which i am working on, i am am trying to insert keys and values into BDB by reading the input records from a input file as below.

Here keys i am inserting as character buffer and for values i am dynamically allocating the memory using malloc and then inserting into bdb.

But when i try to retireve the inserted values from BDB i am geting only the latest record for Value.

    //KEY
    char key[512];
    
    //VALUE
    typedef struct
    {
            char *remValues[10];
    }valuestr;
    
    static valuestr vstr;
    Dbc *dbcObj;
    Dbt dbtKey,dbtValue;
    
    // I am dynamically allocating memory for Value str using malloc example as below.
    vstr.remValues[0] = (char *)malloc(50);
    
    while(!feof(inFile))
    {
    //getting key from input file
    strcpy(buf, <value1>); //value1 comes from input file
    
    //getting Value from input file
    memset(vstr.remValues[0],0,50);
    sprintf(vstr.remValues[0],"%d",<value2>); //value2 comes from input file
    
    ...........
    ...........
        
    //Putting values into BDB
        
    dbtKey.set_data((char*) buf);
    dbtKey.set_size(strlen(buf));
     
    dbtValue.set_data((valuestr*)&vstr);
    dbtValue.set_size(sizeof(valuestr));
     
    int ret = dbObj.put(0,&dbtKey,&dbtValue,DB_NOOVERWRITE);
    }
    
    But when i try to retrive the values from BDB its giving proper values for Key but for Value i am getting only the latest value from input file.
    Below is the Key value retreival code.
    
    dbObj.cursor(NULL,&dbcObj,0);
    char keys[100];
    valuestr *values;
    while(dbcObj->get(&dbtKey,&dbtValue,DB_NEXT)==0)
    {
    memset(keys,0,sizeof(keys));
    strncpy(keys,(char *)dbtKey.get_data(),strlen(buf));
    values = (valuestr *)dbtValue.get_data();
    //printing values and keys on to console
    }

Please help.






Retreving the dynamically allocated values from bdb using C
===

In one of the assignment which i am working on, i am am trying to insert keys and values into BDB by reading the input records from a input file as below.

Here keys i am inserting as character buffer and for values i am dynamically allocating the memory using malloc and then inserting into bdb.

But when i try to retireve the inserted values from BDB i am geting only the latest record for Value.

    //KEY
    char key[512];
    
    //VALUE
    typedef struct
    {
            char *remValues[10];
    }valuestr;
    
    static valuestr vstr;
    Dbc *dbcObj;
    Dbt dbtKey,dbtValue;
    
    // I am dynamically allocating memory for Value str using malloc example as below.
    vstr.remValues[0] = (char *)malloc(50);
    
    while(!feof(inFile))
    {
    //getting key from input file
    strcpy(buf, <value1>); //value1 comes from input file
    
    //getting Value from input file
    memset(vstr.remValues[0],0,50);
    sprintf(vstr.remValues[0],"%d",<value2>); //value2 comes from input file
    
    ...........
    ...........
        
    //Putting values into BDB
        
    dbtKey.set_data((char*) buf);
    dbtKey.set_size(strlen(buf));
     
    dbtValue.set_data((valuestr*)&vstr);
    dbtValue.set_size(sizeof(valuestr));
     
    int ret = dbObj.put(0,&dbtKey,&dbtValue,DB_NOOVERWRITE);
    }
    
    But when i try to retrive the values from BDB its giving proper values for Key but for Value i am getting only the latest value from input file.
    Below is the Key value retreival code.
    
    dbObj.cursor(NULL,&dbcObj,0);
    char keys[100];
    valuestr *values;
    while(dbcObj->get(&dbtKey,&dbtValue,DB_NEXT)==0)
    {
    memset(keys,0,sizeof(keys));
    strncpy(keys,(char *)dbtKey.get_data(),strlen(buf));
    values = (valuestr *)dbtValue.get_data();
    //printing values and keys on to console
    }

Please help.





