3

i have created a .db file using Berkeley DB using C. I want to view the contents inside the .db file. How to achieve this when I ahve no GUI on a linux machine?

user537670
  • 821
  • 6
  • 21
  • 42
  • It'd just be binary garbage with the occasional string in it, but you can `vi` the file directly, or use `od` to dump it out in somewhat more readable hexdump format. – Marc B Aug 22 '11 at 21:18
  • Not having a GUI, usually makes thing easier; not more difficult as you seem to imply. Just read the data (using `get`) and `printf` it. – pmg Aug 22 '11 at 21:18
  • 1
    possible duplicate of [Examining Berkeley DB files from the CLI](http://stackoverflow.com/questions/37644/examining-berkeley-db-files-from-the-cli) – Flimm Sep 11 '13 at 10:57

1 Answers1

0

if you system had been installed the Berkeley DB, you could use it like this, it is a demo to test, hope it could solve your problem:

#include <stdio.h>
#include <stdlib.h>
#include <db.h>

#define DATABASE "test.db"

typedef struct _data_struct {
    int data_id;
    char data[20];
} data_struct;

int main()
{
    DBT key, data;
    DB *dbp;
    int ret;
    data_struct my_data;

    ret = db_create(&dbp, NULL, 0);  // create the DB handle
    if (ret != 0)
    {
        perror("create");
        return 1;
    }

    ret = dbp->open(dbp, NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0);  // open the database
    if (ret != 0)
    {
        perror("open");
        return 1;
    }


    my_data.data_id = 1;
    strcpy(my_data.data, "some data");

    memset(&key, 0, sizeof(DBT));
    memset(&data, 0, sizeof(DBT));

    key.data = &(my_data.data_id);
    key.size = sizeof(my_data.data_id);
    data.data = &my_data;
    data.size = sizeof(my_data);

    ret = dbp->put(dbp, NULL, &key, &data, DB_NOOVERWRITE);  // add the new data into the database
    if (ret != 0)
    {
        printf("Data ID exists\n");
    }

    dbp->close(dbp, 0);   // close the database

    return 0;
}
cyh24
  • 266
  • 1
  • 3
  • 13