Accessing Berkeley DB 5.x from Delphi [versión en español] <Delphi <Indice

What is Berkeley DB? (Wikipedia) - Official page (Oracle site)

Introduction to Berkeley DB

Berkeley DB (BDB) is a computer software library that provides a high-performance embedded database for key/value data.

Accessing from Delphi

Provided that Delphi is capable of directly accessing the BDB engine DLL, it's enough with converting the db.h file that contains the C original access API.

Downloads (version 5.1.25)

Main downloads:
Unit db_h.pas with the Berkeley DB access API (db.h conversion) download
File with the necessary DLLs for using Berkeley DB download
Delphi project with examples of Berkeley DB access download
Supplementary downloads:
Validator of correctness of Delphi conversion download
db.h file from which db_h.pas was created download
Complete Berkeley DB 5.1.25 (installer) download

Compatibility between versions

Each BDB version has its own db.h and an associated DLL (libdb5x.DLL) that implements the database engine. It's very important to take into account that files from different versions can't be mixed.
Also db_h.pas can't be mixed with a DLL from a different version of that db.h from which it was created.

Validate db_h.pas vs db.h

Validation checks:
  • Values of consts.
  • Size of the enumerated types and the values of the elements in the enumeration.
  • Size of data types, including structs/records.
  • Size and position of struct/record fields, including pointers to functions.
    It's very important to check both for preventing possible alignment errors.
If Berkeley DB distribution is installed and a C++ compiler for Windows is available:
  1. Compile bdb_test_c.cpp (uses db.h from the BDB distribution)
    e.g. from Visual Studio Command Prompt: cl /EHsc bdb_test_c.cpp
  2. Run bdb_test_c.exe (it will generate the file bdb_test_c.txt)
If Berkeley DB is not installed or a C++ compiler is not available:
  1. Use the bdb_test_c.txt file included with the validator
In both cases:
  1. Compile bdb_test_pas.dpr with Delphi (it will generate the file bdb_test_pas.exe)
  2. Run bdb_test_pas.exe (it will generate the file bdb_test_pas.txt)
  3. Compare both txt files: fc bdb_test_c.txt bdb_test_pas.txt
  4. The comparison must conclude that both files are identical.

Example of using Berkeley DB API from Delphi

type
  PKey = ^TKey;
  TKey = record
    Id: integer;
  end;

  PData = ^TData;
  TData = record
    Name: array[0..50] of AnsiChar;
  end;
  
var
  FDB: PDB;
  cursor: PDBC;
  dbtKey, dbtData: DBT;
  key: PKey;
  data: PData;
  
begin
  // initialize API
  InitBerkeleyDB;

  // create access handle
  CheckBDB(db_create(FDB, nil, 0));
  try
    // open database
    CheckBDB(FDB.open(FDB, nil, 'Test.db', 'Test', DB_BTREE, DB_CREATE_, 0));

    // create a cursor for retrieving the records in key order
    CheckBDB(FDB.cursor(FDB, nil, @cursor, 0));
    try
      FillChar(dbtKey, Sizeof(DBT), 0);
      FillChar(dbtData, Sizeof(DBT), 0);
  
      // iterate thru all the records and print each one
      while CheckAndFoundBDB(cursor.get(cursor, @dbtKey, @dbtData, DB_NEXT)) do begin
        key := PKey(dbtKey.data);
        data := PData(dbtData.data);
        Memo.Lines.Add('Key: '+IntToStr(key.Id));
        Memo.Lines.Add('Data: '+data.Name);
      end;
    finally
      // free cursor
      CheckBDB(cursor.close(cursor));
    end;
  finally
    // close database and free handle
    CheckBDBandNil(FDB.close(FDB, 0), FDB);
  end;
end;

Page modified 20/3/2021. © 2008-2021 JRL - A Coruña, Spain. All rights reserved.