使用 C 在 berkeley DB 中指定非法标志错误

发布于 2024-12-01 22:34:16 字数 3528 浏览 0 评论 0原文

下面是我的代码:

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

                           #define  DATABASE "access.db"

                       typedef struct {

                   char data1[20];
                   char src[20];
                    } pearson_record;

我收到错误:

为 DB->get 指定了非法标志 DB->get:无效参数

知道我哪里出错了。

                       int
                       main()

                {


                     pearson_record s;
                 char *papa="1.1.1.1";
                 char *source="papa";
                 DB *dbp;
                 DBT key, data;
                 int ret, t_ret;
                 db_recno_t recno;



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


    recno = 10;

#define BUFFER_LENGTH   (5 * 1024 * 1024)
data.ulen = BUFFER_LENGTH;
    data.flags = DB_DBT_USERMEM;

    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 = &recno;
    key.size = sizeof(recno);
    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,0)) == 0)
    printf("db: %d: key stored.\n", *(int *)key.data);
    else
    {
        dbp->err(dbp, ret, "DB->put");
        goto err;
    }
    recno = 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 = &recno;
    key.size = sizeof(recno);
    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,0)) == 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_SET_RECNO)) == 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_SET_RECNO)) == 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);
}

Below is my code:

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

                           #define  DATABASE "access.db"

                       typedef struct {

                   char data1[20];
                   char src[20];
                    } pearson_record;

I am geting the error:

illegal flag specified to DB->get
DB->get: Invalid argument

Any idea where I am going wrong.

                       int
                       main()

                {


                     pearson_record s;
                 char *papa="1.1.1.1";
                 char *source="papa";
                 DB *dbp;
                 DBT key, data;
                 int ret, t_ret;
                 db_recno_t recno;



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


    recno = 10;

#define BUFFER_LENGTH   (5 * 1024 * 1024)
data.ulen = BUFFER_LENGTH;
    data.flags = DB_DBT_USERMEM;

    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 = &recno;
    key.size = sizeof(recno);
    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,0)) == 0)
    printf("db: %d: key stored.\n", *(int *)key.data);
    else
    {
        dbp->err(dbp, ret, "DB->put");
        goto err;
    }
    recno = 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 = &recno;
    key.size = sizeof(recno);
    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,0)) == 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_SET_RECNO)) == 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_SET_RECNO)) == 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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

删除会话 2024-12-08 22:34:16

快速浏览一下 DB->get< 的文档/a> 表示:“要指定 DB_SET_RECNO,底层数据库必须是 Btree 类型,并且必须是使用 DB_RECNUM 标志创建的。”

看起来您没有使用该标志创建数据库。我还没有查看你的其余代码,但这是一个明显需要修复的地方。

您还可以在在线Berkeley DB 论坛上提出这些问题。

问候,

戴夫

A quick glance at the documentation for DB->get says: "For DB_SET_RECNO to be specified, the underlying database must be of type Btree, and it must have been created with the DB_RECNUM flag."

It doesn't look like you created the database with that flag. I haven't looked through the rest of your code, but that's one obvious place that needs to be fixed.

You can also ask these questions on the online Berkeley DB forum.

Regards,

Dave

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文