开发者

Not able to access multiple data inside a database created by Berkeley DB using C

开发者 https://www.devze.com 2023-03-31 20:12 出处:网络
I am trying to develop a database using Berkeley Db in C. I want to have multiple data inside the database and then access them. my code is below:

I am trying to develop a database using Berkeley Db in C. I want to have multiple data inside the database and then access them. my code is below:

#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <db.h>

#define DATABASE "access.db"

开发者_C百科typedef struct {
    int id;
    char data1[20];
    char src[20];
} pearson_record;

int
main()
{
    pearson_record s;
    char *papa="1.1.1.1";
    char *source="papa";
    DB *dbp;
    DBT key, data;
    int ret, t_ret;

    if ((ret = db_create(&dbp, NULL, 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);
        goto err;
    }

    s.id = 10;

    strncpy(s.data1, papa, strlen(papa)+1);
    strncpy(s.src, source, strlen(source)+1);

    memset(&key, 0, sizeof(key));
    memset(&data, 0, sizeof(data));
    //memset(&s, 0, sizeof(struct pearson_record));
    key.data = &(s.id);
    key.size = sizeof(int);
    data.data = &s;
    data.size = sizeof(s);

    papa="1.1.1.2";
    source="papaa";
    strncpy(s.data1, papa, strlen(papa)+1);
    strncpy(s.src, source, strlen(source)+1);

    if ((ret = dbp->put(dbp, NULL, &key,&data,DB_NOOVERWRITE)) == 0)
        printf("db: %d: key stored.\n", *(int *)key.data);
    else
    {
        dbp->err(dbp, ret, "DB->put");
        goto err;
    }
    s.id = 11;

    strncpy(s.data1, papa, strlen(papa)+1);
    strncpy(s.src, source, strlen(source)+1);

    memset(&key, 0, sizeof(key));
    memset(&data, 0, sizeof(data));
    //memset(&s, 0, sizeof(struct pearson_record));
    key.data = &(s.id);
    key.size = sizeof(int);
    data.data = &s;
    data.size = sizeof(s);

    papa="1.1.1.2";
    source="papaa";
    strncpy(s.data1, papa, strlen(papa)+1);
    strncpy(s.src, source, strlen(source)+1);

    if ((ret = dbp->put(dbp, NULL, &key,&data,DB_NOOVERWRITE)) == 0)
        printf("db: %d: key stored.\n", *(int *)key.data);
    else
    {
        dbp->err(dbp, ret, "DB->put");
        goto err;
    }
    pearson_record *ppr;
    if ((ret = dbp->get(dbp, NULL, &key, &data, DB_MULTIPLE)) == 0) {
        ppr = (pearson_record *) data.data;
        printf("db: %d: key retrieved: data was %s,%s. %d\n",
        *(int *)key.data, ppr->data1,ppr->src, data.size);
    } else {
        dbp->err(dbp, ret, "DB->get");
        goto err;
    }

    if ((ret = dbp->get(dbp, NULL, &key, &data, DB_MULTIPLE)) == 0) {
        ppr = (pearson_record *) data.data;
        printf("db: %d: key retrieved: data was %s,%s. %d\n",
        *(int *)key.data, ppr->data1,ppr->src, data.size);
    } else {
        dbp->err(dbp, ret, "DB->get");
        goto err;
    }

    err:
    if ((t_ret = dbp->close(dbp, 0)) != 0 && ret == 0)
        ret = t_ret; 
    exit(ret);
}

Right now it says DB_DBT_MULTIPLE has to be set. I don't know how to do that. Previously without the flags i was only getting the last entry. Any kind of help would be appreciated. Thanks in advance


If I'm reading your code correctly, you are calling get twice with the same key, which returns the same data for obvious reasons.

If you want to iterate over the records, you will need to use a Cursor, see Chapter 4 of Getting Started with Berkeley DB for a good reference for same.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号