如下,使用gomb库

 

结果如下:

10
Key-0: Val-0
Key-1: Val-1
Key-2: Val-2
Key-3: Val-3
Key-4: Val-4
Key-5: Val-5
Key-6: Val-6
Key-7: Val-7
Key-8: Val-8
Key-9: Val-9
Val-3
*********range begin*********
Key-2: Val-2
Key-3: Val-3
Key-4: Val-4
Key-5: Val-5
**********range end********

 

尼玛,还是参考BDB写出来的!

 

参考:https://stackoverflow.com/questions/18707751/retrieving-a-range-of-data-from-berkeley-db

BDB的例子:

void get(DB *dbp, int key1, int key2){
  DBC *curs;
  DBT k,v;
  int fl;

  // Get a cursor
  dbp->cursor(dbp, NULL, &curs, 0);
  if (!curs) _dberr("can't get a cursor");

  // Set DBT for 1st key and value
  memset(&v, 0, sizeof(DBT));
  memset(&k, 0, sizeof(DBT));
  k.data = &key1;
  k.size = sizeof(key1);

  fl = DB_SET_RANGE; // first key will be >=key1
  while (curs->c_get(curs, &k, &v, fl)==0 &&
         key2 >= *(int *)k.data){
    fl = DB_NEXT;
    // use v.data
  }
}