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); |