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)
|Unit db_h.pas with the Berkeley DB access API (db.h conversion)
|File with the necessary DLLs for using Berkeley DB
|Delphi project with examples of Berkeley DB access
|Validator of correctness of Delphi conversion
|db.h file from which db_h.pas was created
|Complete Berkeley DB 5.1.25 (installer)
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
- 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:
- 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
- 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:
- Use the bdb_test_c.txt file included with the validator
In both cases:
- Compile bdb_test_pas.dpr with Delphi (it will generate the file bdb_test_pas.exe)
- Run bdb_test_pas.exe (it will generate the file bdb_test_pas.txt)
- Compare both txt files: fc bdb_test_c.txt bdb_test_pas.txt
- The comparison must conclude that both files are identical.
Example of using Berkeley DB API from Delphi
PKey = ^TKey;
TKey = record
PData = ^TData;
TData = record
Name: array[0..50] of AnsiChar;
dbtKey, dbtData: DBT;
// initialize API
// create access handle
CheckBDB(db_create(FDB, nil, 0));
// 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));
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);
// free cursor
// close database and free handle
CheckBDBandNil(FDB.close(FDB, 0), FDB);