dbTalk Databases Forums  

Cannot read from database correctly

comp.databases.berkeley-db comp.databases.berkeley-db


Discuss Cannot read from database correctly in the comp.databases.berkeley-db forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Sam
 
Posts: n/a

Default Cannot read from database correctly - 03-20-2008 , 01:03 AM






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.

Reply With Quote
  #2  
Old   
Sam
 
Posts: n/a

Default Re: Cannot read from database correctly - 03-22-2008 , 10:20 AM






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.

Reply With Quote
  #3  
Old   
Sam
 
Posts: n/a

Default Re: Cannot read from database correctly - 03-22-2008 , 10:20 AM



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.

Reply With Quote
  #4  
Old   
Sam
 
Posts: n/a

Default Re: Cannot read from database correctly - 03-22-2008 , 10:20 AM



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.

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.