2

I am trying to write a program that uses a Berkeley Database and i configured my database as followed :

DatabaseConfig dbConfig = new DatabaseConfig();

dbConfig.setType(DatabaseType.HASH);
dbConfig.setAllowCreate(true);
dbConfig.setSortedDuplicates(false); //tried commented this out as well

// Create the database
std_db = new Database("name.db", null, dbConfig); //delcared as a private variable

I have been spending the last hour searching the web for what happens with a key/data pair when a new key to be entered matches an existing one. According to the API the current data will be overwritten with the new data, but in my program it seems that this is not the case. Anyone know how to make it so that if im putting into a database and the key exists it will overwrite the old data?

I was thinking of just deleting the old key first and then putting in the new key/data combo, but this solution is a work-around and just adds unnecessary overhead.

Thanks in Advance :)

user597608
  • 387
  • 8
  • 20

1 Answers1

1

I think database.put() is the solution. According to below link:

The Db::put method stores key/data pairs in the database. The default behavior of the Db::put function is to enter the new key/data pair, replacing any previously existing key if duplicates are disallowed, or adding a duplicate data item if duplicates are allowed. If the database supports duplicates, the Db::put method adds the new data value at the end of the duplicate set. If the database supports sorted duplicates, the new data value is inserted at the correct sorted location.

http://web.deu.edu.tr/doc/berkeley/berkeleyDB/api_cxx/db_put.html

HRgiger
  • 2,750
  • 26
  • 37