1 Reply Latest reply: May 3, 2012 10:24 PM by 792937 RSS

    How to insert autoincrement record and query it with multiple keys

    792937
      Hi all,

      I am not familiar with BerkeyleyDB, I am now doing a project that needs to insert records and then query it out with multiple keys at later time. Since there is no field that can be distinct primary key, I want to user DbSequence as auto increment primary key to db, and set other index keys as secondary db, then using join cursor to do query with multiple keys.

      I don't know how to use DbSequence, can anyone direct me to a example of using DbSequence as auto increment primary key?

      Regards
      -Bruce
        • 1. Re: How to insert autoincrement record and query it with multiple keys
          792937
          I figured out the method to insert record with auto increment primary key. I listed the code block below:

          char m_SeqNamePositions[32] = "MyPositions";
          DbSequence *m_pSeqPositions;

          m_pDBPositions = new Db(NULL, 0);
                    m_pDBPositions->open(NULL, pszFileName, szFileName, DB_BTREE, DB_CREATE, 0);          // 无数据文件
                    m_pSeqPositions = new DbSequence(m_pDBPositions, 0);
                    Dbt key((void *)m_SeqNamePositions, (u_int32_t)strlen(m_SeqNamePositions));
                    m_pSeqPositions->open(NULL, &key, DB_CREATE);

          db_seq_t SeqNum;
               m_pSeqPositions->get(0, 1, &SeqNum, 0);
               Dbt key((void *)&SeqNum, (u_int32_t)sizeof(SeqNum));
               Dbt data(pRecord, sizeof(*pRecord));
               return m_pDBPositions->put(NULL, &key, &data, DB_NOOVERWRITE);

          m_pSeqPositions->close(0);
                    m_pDBPositions->close(0);
          delete m_pSeqPositions;
          delete m_pDBPositions;
                    m_pDBPositions = NULL;