Discussion:
Cannot read from database correctly
(too old to reply)
Sam
2008-03-20 06:03:14 UTC
Permalink
Hi all,

I'm new to Berkeley DB. I'm trying to write a simple program to create
a very simple database and then read the data from it.
I built the Berkeley DB binary correctly and I don't have any syntax
or linking errors.
This is the code that I wrote (almost all of it from the "Getting
Started with Data Storage" Guide).



#include <sys/types.h>

#include <iostream>
#include <iomanip>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <db_cxx.h>

using std::cin;
using std::cout;
using std::cerr;

void main()
{
Db db(NULL, 0); // Instantiate the Db object
u_int32_t oFlags = DB_CREATE; // Open flags;
try {
db.open(NULL,"my_db.db",NULL, DB_HASH, oFlags, 0); // Open the
database

} catch(DbException &e) {
std::cerr << "Error opening database: " << "my_db.db" << "\n";
std::cerr << e.what() << std::endl;
} catch(std::exception &e) {
std::cerr << "Error opening database: " << "my_db.db" << "\n";
std::cerr << e.what() << std::endl;
}


char *description = "Grocery bill.";
float money = 122.45;


Dbt key(&money, sizeof(float));
Dbt data(description, strlen(description) + 1);

int ret = db.put(NULL, &key, &data, DB_NOOVERWRITE);
if (ret == DB_KEYEXIST) {
db.err(ret, "Put failed because key %f already exists", money);
}

try {
// Close the database
db.close(0);
// DbException is not subclassed from std::exception, so
// need to catch both of these.
} catch(DbException &e) {
// Error handling code goes here
} catch(std::exception &e) {
// Error handling code goes here
}


Db db_1(NULL, 0); // Instantiate the Db object
u_int32_t cFlags = DB_RDONLY;
try {
db_1.open(NULL,"my_db.db",NULL, DB_HASH, cFlags, 0); // Open the
database

} catch(DbException &e) {
std::cerr << "Error opening database: " << "my_db.db" << "\n";
std::cerr << e.what() << std::endl;
} catch(std::exception &e) {
std::cerr << "Error opening database: " << "my_db.db" << "\n";
std::cerr << e.what() << std::endl;
}


char *description_1[13 + 1];
Dbt key_1, data_1;


key_1.set_data(&money);
key_1.set_ulen(sizeof(float));

data_1.set_data(&description_1);
data_1.set_ulen(13 + 1);
data_1.set_flags(DB_DBT_USERMEM);
db_1.get(NULL, &key_1, &data_1, 0);

cout << (char *)data_1.get_data() << "\n";
cout << key_1.get_data() << "\n";

}



Basically, I open the database and write a single record then try to
read that record again from the database.
What is happening to me is:
Once I write the record, and read it; it does not print the data
correctly even the key is not printed correctly. All what I see is
garbage.
Can someone points out what is wrong here

Any help is appreciated.
Sam
2008-03-22 15:20:14 UTC
Permalink
I know how to fix it now.

instead of

key_1.set_data(&money);
key_1.set_ulen(sizeof(float));

we put:

key_1.set_data(&money);
key_1.set_size(sizeof(float));

and to fix the key printing we need to write:

cout << *((float*)key_1.get_data()) << "\n";

instead of:

cout << key_1.get_data() << "\n";




I hope this help someone else.

Loading...