2 Replies Latest reply: Apr 15, 2013 5:30 AM by 1001655 RSS

    Unable to insert new records in DB

    1001655
      Hi All,

      I am a newbie to Berkeley DB . I am creating an application and as part of it I am trying to store key,value pairs (mostly String values) in the data base. But whenever I try to insert a new key,Value pair it is getting overridden or replaced by already present key value pair.

      Here is how it goes....

      I already have "Key : Value | user : user is testing" pair in my DB.

      I have written a following function to insert a new record into my DB

      public String insertRecord(String newKey, String newValue) throws DatabaseException, FileNotFoundException, UnsupportedEncodingException
           {
                Environment myEnv = null;
                Database myDb = null;
                Cursor myCursor = null;
                try
                {
                     
                     File envHome = new File("H:/BDB"); // directory of my environment
                     EnvironmentConfig envConf = new EnvironmentConfig();
                     envConf.setAllowCreate(true);
                     envConf.setInitializeCache(true);
                     myEnv = new Environment(envHome, envConf);
                     
                     
                     DatabaseConfig dbConfig = new DatabaseConfig();
                     dbConfig.setAllowCreate(true);
                     dbConfig.setType(DatabaseType.BTREE);
                     myDb = myEnv.openDatabase(null, "ScriptDBFile.db","ScriptDB", dbConfig); // ScriptDBFile.db is my DB file name and ScriptDB is my DB Name
                     
                     // Create keys and data
                     String key = newkey;
                     String data = newValue;
                     
                     
                     DatabaseEntry theKey = new DatabaseEntry(key.getBytes("UTF-8"));
                     DatabaseEntry theData = new DatabaseEntry(data.getBytes("UTF-8"));
                     
                     
                     myCursor = myDb.openCursor(null, null);
                     OperationStatus retVal = myCursor.put(theKey, theData);
                     
                     
                }
                catch(DatabaseException de)
                {
                     System.out.println(de.getMessage());
                     String errorMessage = "There is a databse Exceptions";
                     throw new DatabaseException(errorMessage);
                }
                catch(FileNotFoundException fnfe)
                {
                     System.out.println(fnfe.getMessage());
                     String errorMessage = "Specified File Not Found";
                     throw new FileNotFoundException(errorMessage);
                }
                catch(UnsupportedEncodingException uee)
                {
                     System.out.println(uee.getMessage());
                     String errorMessage = "Encoding not Supported";
                     throw new UnsupportedEncodingException(errorMessage);
                }
                finally
                {
                     try
                     {
      if(myCursor != null)
                               myCursor.close();                              
                          if(myDb != null)
                               myDb.close();                    
                          if(myEnv != null)
                               myEnv.close();
                     }
                     catch(DatabaseException de)
                     {
                          System.out.println(de.getMessage());
                     }
                }
                return "Script insert";
      }


      To insert a new record I am calling the above fucntion as insertRecord("Bob" , "Bob is good"); which inserts the record into DB but when I print the records its showing me


      user : user is testing (No changes for this record)
      bobr : bob is goodting

      Here the new record(both key and value) which I tried to insert is getting appended with the remaining chars of the old record.
      i.e., bob is replacing 'use' of user and the remaining chars of old key (r here of user) are appending to new key(bob) giving the output as 'bobr'. Same is the case with value.

      I am unable to figure out this issue.
      Any help is highly appreciated.
        • 1. Re: Unable to insert new records in DB
          user780404
          Hi,

          We did some testing but could not reproduce the issue you mentioned with BDB v5.3.21. I pasted my test program based on your code bellow for you to try.

          Additionally, a suggestion for your program is that setting up the dbenv and database files should be done once outside insertRecord() function, not everytime a record is inserted. Any problem let me know..

          ===================
          public class TestInsertRecord {
               
          public static String insertRecord(String newKey, String newValue)
          throws DatabaseException, FileNotFoundException, UnsupportedEncodingException {
          Environment myEnv = null;
          Database myDb = null;
          Cursor myCursor = null;
          try
          {
          File envHome = new File("./db"); // directory of my environment
          EnvironmentConfig envConf = new EnvironmentConfig();
          envConf.setAllowCreate(true);
          envConf.setInitializeCache(true);
          myEnv = new Environment(envHome, envConf);

          DatabaseConfig dbConfig = new DatabaseConfig();
          dbConfig.setAllowCreate(true);
          dbConfig.setType(DatabaseType.BTREE);
          myDb = myEnv.openDatabase(null, "ScriptDBFile.db", "ScriptDB", dbConfig); // ScriptDBFile.db is my DB file name and ScriptDB is my DB Name

          // Create keys and data
          String key = newKey;
          String data = newValue;

          DatabaseEntry theKey = new DatabaseEntry(key.getBytes("UTF-8"));
          DatabaseEntry theData = new DatabaseEntry(data.getBytes("UTF-8"));

          myCursor = myDb.openCursor(null, null);
          OperationStatus retVal = myCursor.put(theKey, theData);

          } catch(DatabaseException de) {
          System.out.println(de.getMessage());
          String errorMessage = "There is a databse Exceptions";
          throw new DatabaseException(errorMessage);
          } catch(FileNotFoundException fnfe) {
          System.out.println(fnfe.getMessage());
          String errorMessage = "Specified File Not Found";
          throw new FileNotFoundException(errorMessage);
          } catch(UnsupportedEncodingException uee) {
          System.out.println(uee.getMessage());
          String errorMessage = "Encoding not Supported";
          throw new UnsupportedEncodingException(errorMessage);
          } finally {
          try {
          if(myCursor != null)
          myCursor.close();
          if(myDb != null)
          myDb.close();
          if(myEnv != null)
          myEnv.close();
          } catch(DatabaseException de){
          System.out.println(de.getMessage());
          }
          }
          return "Script insert";
          }

          private static void showAllEntries() {
               Environment myEnv = null;
          Database myDb = null;
          Cursor myCursor = null;
          try {
               File envHome = new File("./db"); // directory of my environment
               EnvironmentConfig envConf = new EnvironmentConfig();
               envConf.setAllowCreate(true);
               envConf.setInitializeCache(true);
               myEnv = new Environment(envHome, envConf);
               
               DatabaseConfig dbConfig = new DatabaseConfig();
               dbConfig.setAllowCreate(true);
               dbConfig.setType(DatabaseType.BTREE);
               myDb = myEnv.openDatabase(null, "ScriptDBFile.db", "ScriptDB", dbConfig); // ScriptDBFile.db is my DB file name and ScriptDB is my DB Name
               myCursor = myDb.openCursor(null, null);
               
               DatabaseEntry rkey = new DatabaseEntry();
               DatabaseEntry rdata = new DatabaseEntry();
               OperationStatus ret;
               for (ret = myCursor.getFirst(rkey, rdata, LockMode.DEFAULT);
               ret == OperationStatus.SUCCESS;
               ret = myCursor.getNext(rkey, rdata, LockMode.DEFAULT)) {
               String keystr = new String
               (rkey.getData(), rkey.getOffset(), rkey.getSize());
               String datastr = new String
               (rdata.getData(), rdata.getOffset(), rdata.getSize());
               System.out.println("\t"+keystr+"\t"+datastr);
               }
          } catch (Exception ex) {
               ex.printStackTrace();
          } finally {
          try {
          if(myCursor != null)
          myCursor.close();
          if(myDb != null)
          myDb.close();
          if(myEnv != null)
          myEnv.close();
          } catch(DatabaseException de){
          System.out.println(de.getMessage());
          }
          }
          }

          public static void main(String[] argv) {
          try {
                    insertRecord("user", "user is testing");
                         insertRecord("bob", "bob is good");
                         showAllEntries();
                    } catch (FileNotFoundException | UnsupportedEncodingException
                              | DatabaseException e) {
                         e.printStackTrace();
                    }
          }
          }

          Regards,
          -Jin
          • 2. Re: Unable to insert new records in DB
            1001655
            Hi Jin,

            It worked like a charm. Hats off to you and thanks a lot.
            As you suggested I made a separate setup function for setting up the dbEnv and database files and its working how I wanted now. Once again thanx alot.

            Thanks,
            Vikash