关注开源代码的实际应用
Berkeley DB是个优秀的嵌入式数据库Library,适合用于开发一些独立的Desktop程序。下面介绍如何获取、使用该Library。
①、下载、安装:
可以从www.sleepycat.com下载;或者google一下关键字berkeley db就可以找到了。下载下来的是一个.msi安装包,安装就行了。
我将它安装在D:\Program Files\Sleepycat Software\Berkeley DB 4.4.20\。
②、编译:
在D:\Program Files\Sleepycat Software\Berkeley DB
4.4.20\db-4.4.20\build_win32\目录下有个Berkeley_DB.dsw(VC++ 6
workspace文件),用VC++2003打开并转换成VS.net的solution。
然后,分别编译Debug版和Release版各一份。
我关注的是Static Library。其中D:\Program Files\Sleepycat Software\Berkeley DB
4.4.20\db-4.4.20\build_win32\Debug\libdb44sd.lib为Static Library for
debug;而D:\Program Files\Sleepycat Software\Berkeley DB
4.4.20\db-4.4.20\build_win32\Release\libdb44s.lib为Static Library for
Release。
③、使用已经编译好了的Static Library:
新建一个Win32 Console项目,须要进行如下设置才可以正确编译:
首先设置Debug版的Console:
A、在【配置属性→C/C++→常规→附加包含目录】中设置“D:\Program Files\Sleepycat Software\Berkeley DB 4.4.20\include”;
B、
在【配置属性→链接器→输入→附加依赖项】中设置 "D:\Program Files\Sleepycat Software\Berkeley
DB 4.4.20\db-4.4.20\build_win32\Debug\libdb44sd.lib"。一定要在文件路径前后加上双引号。
C、在【配置属性→C/C++→代码生成→运行时库】中设置为 〖多线程调试 DLL (/MDd)〗。否则将会引发LNK 2005链接错误。
可以参考http://www.openh323.org/pipermail/openh323/2003-February/060003.html (Solving the LNK2005 error ):
To avoid this link error, We have to change [Project]-[Setting]-[C/C++],
Category -> Code Generation,
Use run-time library -> Debug Multithreaded DLL (change this!)
The default setting of 'Use run-time labrary' is 'Debug Single-Threaded'.
That is incorrect...
然后,对Release进行类似的设置。
④、测试:
A、用C API:
- // TestBDB.cpp :
- //
- #include "stdafx.h"
- #include <db.h>
- int _tmain(int argc, _TCHAR* argv[])
- {
- DB *dbp; /* DB structure handle */
- u_int32_t flags; /* database open flags */
- int ret; /* function return value */
- /* Initialize the structure. This
- * database is not opened in an environment,
- * so the environment pointer is NULL. */
- ret = db_create(&dbp, NULL, 0);
- if (ret != 0) {
- /* Error handling goes here */
- }
- /* Database open flags */
- flags = DB_CREATE; /* If the database does not exist,
- * create it.*/
- /* open the database */
- ret = dbp->open(dbp, /* DB structure pointer */
- NULL, /* Transaction pointer */
- "my_db.db", /* On-disk file that holds the database. */
- NULL, /* Optional logical database name */
- DB_BTREE, /* Database access method */
- flags, /* Open flags */
- 0); /* File mode (using defaults) */
- if (ret != 0) {
- /* Error handling goes here */
- }
- if( dbp != NULL)
- dbp->close(dbp,0);
- return 0;
- }
B、用C++ API:
- #include "stdafx.h"
- #include <db_cxx.h>
- int _tmain(int argc,_TCHAR* argv[])
- {
- Db db(NULL, 0); // Instantiate the Db object
- u_int32_t oFlags = DB_CREATE; // Open flags;
- try {
- // Open the database
- db.open(NULL, // Transaction pointer
- "my_db2.db", // Database file name
- NULL, // Optional logical database name
- DB_BTREE, // Database access method
- oFlags, // Open flags
- 0); // File mode (using defaults)
- // DbException is not subclassed from std::exception, so
- // need to catch both of these.
- } catch(DbException &e) {
- // Error handling code goes here
- } catch(std::exception &e) {
- // Error handling code goes here
- }
- try {
- // Close the database
- db.close(0);
- // DbException is not subclassed from std::exception, so
- // need to catch both of these.
- } catch(DbException &e) {
- // Error handling code goes here
- } catch(std::exception &e) {
- // Error handling code goes here
- }
- }
⑤、进一步学习:
目前手头上唯一的资料是:D:\Program Files\Sleepycat Software\Berkeley DB 4.4.20\docs下的那些帮助文档;我想这应当也是最好的学习资料了。