Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6382457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:31:26+00:00 2026-05-25T02:31:26+00:00

I am trying to develop a database using Berkeley Db in C. I want

  • 0

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"

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:31:27+00:00Added an answer on May 25, 2026 at 2:31 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to develop some RESTful web services which have to access some
i'm trying to develop an application but then these warnings popped out. I have
I am trying to develop a .NET Web Project using NHibernate and Spring.NET, but
I am trying to develop a iphone app by using cocos2d. I create a
I am trying to develop a complex textual search engine. I have thousands of
I am trying to develop a database model for a recruitment website and I
I'm trying to develop a system, and doing the ER diagram now, i have
I am trying to develop a project in C (on Dev-Cpp IDE) using SQLite
I am trying to develop a database for a website. This website has registered
I'm trying to develop a table that can be used to edit database information

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.