开发者

How to view a ".db file in linux with no GUI

开发者 https://www.devze.com 2023-03-29 18:12 出处:网络
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开发者_JAVA技巧 ahve no GUI on a linux machine?if you system had been in

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开发者_JAVA技巧 ahve no GUI on a linux machine?


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

精彩评论

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