无法使用C访问由Berkeley DB创建的数据库中的多个数据

时间:2011-08-30 02:07:41

标签: c berkeley-db

我正在尝试使用C中的Berkeley Db开发数据库。我希望在数据库中有多个数据然后访问它们。我的代码如下:

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

#define DATABASE "access.db"

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);
}

现在它说必须设置DB_DBT_MULTIPLE。我不知道该怎么做。以前没有旗帜我只是最后一个条目。任何形式的帮助将不胜感激。提前致谢

1 个答案:

答案 0 :(得分:1)

如果我正确地阅读了您的代码,您使用相同的密钥调用get两次,这显然会返回相同的数据。

如果要迭代记录,则需要使用Cursor,请参阅Chapter 4 of Getting Started with Berkeley DB以获得相同的参考。