Day: January 21, 2020

Testing QDBM: Quick Database Manager

The QDBM is a small database manager that could be used for embedded systems (just pay attention on its license: LGPL, so only dynamic linkage if you are using it for commercial applications).

Fortunately the documentation of the project is very good:

https://fallabs.com/qdbm/

The most basic example is demonstrated on project documentation:

#include <depot.h>
#include <stdlib.h>
#include <stdio.h>

#define NAME     "mikio"
#define NUMBER   "000-1234-5678"
#define DBNAME   "book"

int main(int argc, char **argv){
  DEPOT *depot;
  char *val;

  /* open the database */
  if(!(depot = dpopen(DBNAME, DP_OWRITER | DP_OCREAT, -1))){
    fprintf(stderr, "dpopen: %s\n", dperrmsg(dpecode));
    return 1;
  }

  /* store the record */
  if(!dpput(depot, NAME, -1, NUMBER, -1, DP_DOVER)){
    fprintf(stderr, "dpput: %s\n", dperrmsg(dpecode));
  }

  /* retrieve the record */
  if(!(val = dpget(depot, NAME, -1, 0, -1, NULL))){
    fprintf(stderr, "dpget: %s\n", dperrmsg(dpecode));
  } else {
    printf("Name: %s\n", NAME);
    printf("Number: %s\n", val);
    free(val);
  }

  /* close the database */
  if(!dpclose(depot)){
    fprintf(stderr, "dpclose: %s\n", dperrmsg(dpecode));
    return 1;
  }

  return 0;
}

To compile this example you need to install the libqdbm:

$ sudo apt install libqdbm-dev

Save the above code as phone.c and compile this way:

$ gcc -std=c99 -o phone phone.c $(pkg-config --cflags --libs qdbm)

You can also install the package qdbm-util:

$ sudo apt install qdbm-util

Now you can create the database using this command:

$ dpmgr create mydata.qdbm

To insert data:

$ dpmgr put mydata.qdbm “Joe” “Student”

$ dpmgr put mydata.qdbm “Mary” “Doctor”

$ dpmgr put mydata.qdbm “Julia” “Actress”