dbTalk Databases Forums  

how to update a key/data pair

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


Discuss how to update a key/data pair in the comp.databases.berkeley-db forum.



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

Default how to update a key/data pair - 09-27-2003 , 12:31 PM






Hey again,

I´m trying to update the data of a specific key/data pair. The problem is
that i does not do that. I use the put() method to store the new DBT in the
database. The only thing I do is changing the data part of the DBT and stor
it again.
Is this the right way or should I delete the specific DBT from the database
and store a new DBT but with the same Key?

Here is my code (the code compiles and links properly, but does not update
the DBT):

Db db(0,0);
db.open(NULL,"the_db.db",NULL,DB_BTREE,DB_CREATE,0 664);

try
{
Dbt key;
Dbt data;
memset(&key,0,sizeof(key));
memset(&data,0,sizeof(data));
key.set_data("0");
key.set_size(sizeof("0"));

ret = db.get(NULL,&key,&data,0);
if(ret == 0)
{
char *the_data = (char *)data.get_data();
}
else if(ret = EINVAL)
{
std::cout << "error: EINVAL" << "\n";
}

data.set_data("0");
data.set_size(sizeof("0"));

ret = db.put(0,&key,&data,0);
if(ret == 0)
{
std::cout << "updated succesfull.\n";
}
else if(ret == EACCES)
{
std::cout << "error: EACCES\n";
}
else if(ret == EINVAL)
{
std::cout << "error: EINVAL\n";
}
}
catch (DbException &dbe) {
std::cerr << "Exception: " << dbe.what() << "\n";
}
db.close(0);


Thanks in advance!
/Bogdan



Reply With Quote
  #2  
Old   
Bogdan Sarbu
 
Posts: n/a

Default Re: how to update a key/data pair - 09-27-2003 , 05:38 PM






Hey,

I have found a way around the earlier problem, but now I have another
problem.
When I retrieve a specific DBT and want to display the data (from the
key/data pair) then it only shows a part of the string I stored in the
database.

Here is my code:

//=========================================
//Storing the DBT
//=========================================

int ret;

Db db(0,0);
db.set_error_stream(&std::cerr);
db.set_errpfx("Persistens");
db.set_pagesize(1024); /* Page size: 1K. */
db.set_cachesize(0,32 * 1024,0);
db.open(NULL,"Test.db",NULL,DB_BTREE,DB_CREATE,066 4);

try
{
Dbt key;
Dbt data;
memset(&key,0,sizeof(key));
memset(&data,0,sizeof(data));
key.set_flags(DB_DBT_MALLOC);
data.set_flags(DB_DBT_MALLOC);

key.set_data("Test");
key.set_size(sizeof("Test"));
data.set_data(indData);
data.set_size(sizeof(indData));

ret = db.put(0,&key,&data,0);
if(ret == 0)
{
std::cout << "Saved: " << indData << "\n";
}
}
catch (DbException &dbe) {
std::cout << "Persistens: " << dbe.what() << "\n";
}

db.sync(0);
db.close(0);


//=========================================
//Retrieving the DBT
//=========================================

int ret;

Db db(0,0);
db.open(NULL,"Test.db",NULL,DB_BTREE,DB_CREATE,066 4);

try
{
Dbt key;
Dbt data;
memset(&key,0,sizeof(key));
memset(&data,0,sizeof(data));
key.set_flags(DB_DBT_MALLOC);
data.set_flags(DB_DBT_MALLOC);

Dbc *dbcp;
db.cursor(NULL, &dbcp, 0);

key.set_data("Test");
key.set_size(sizeof("Test"));

ret = dbcp->get(&key, &data, DB_SET);
if(ret == 0)
{
char *text = (char *)data.get_data();
std::cout << text << std::endl;
}

dbcp->close();
}
catch (DbException &dbe) {
std::cout << "Persistens: " << dbe.what() << "\n";
}

db.close(0);



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.