Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Berkely DB > Cannot read fro...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 2 Topic 1793 of 1800
Post > Topic >>

Cannot read from database correctly

by Sam <salsubaiee@[EMAIL PROTECTED] > Mar 19, 2008 at 11:03 PM

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.
 




 2 Posts in Topic:
Cannot read from database correctly
Sam <salsubaiee@[EMAIL  2008-03-19 23:03:14 
Re: Cannot read from database correctly
Sam <salsubaiee@[EMAIL  2008-03-22 08:20:14 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan13V112 Sun Jul 6 16:16:28 CDT 2008.