文章分类

站点统计

  • 分类总数: 13 个
  • 文章总数: 145 篇
  • 评论总数: 47 条
  • 附件总数: 59 个
  • 建站日期: 2008-08-18
  • 访问总数: 472872 人次
  • RSS订阅: 文章|评论

关于Berkeley DB在VC++2003中的使用

Admin 于 2008-09-11 12:58:18 发表C/C++

订阅: http://www.kaiyuan8.org/Feed/Article_30.aspx
引用: 点这里获取地址 (UTF-8)
Db4o性能优化1 < 关于Berkeley DB在VC++2003中的使用 > VC巨资注入开源软件产业 商业模式仍需摸索

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:

  1. // TestBDB.cpp : 
  2. // 
  3.  
  4. #include "stdafx.h" 
  5. #include <db.h> 
  6.  
  7. int _tmain(int argc, _TCHAR* argv[]) 
  8.  DB *dbp;           /* DB structure handle */ 
  9.  u_int32_t flags;   /* database open flags */ 
  10.  int ret;           /* function return value */ 
  11.  
  12.  /* Initialize the structure. This 
  13.  * database is not opened in an environment, 
  14.  * so the environment pointer is NULL. */ 
  15.  ret = db_create(&dbp, NULL, 0); 
  16.  if (ret != 0) { 
  17.   /* Error handling goes here */ 
  18.  } 
  19.  
  20.  /* Database open flags */ 
  21.  flags = DB_CREATE;    /* If the database does not exist, 
  22.         * create it.*/ 
  23.  
  24.  /* open the database */ 
  25.  ret = dbp->open(dbp,        /* DB structure pointer */ 
  26.   NULL,       /* Transaction pointer */ 
  27.   "my_db.db"/* On-disk file that holds the database. */ 
  28.   NULL,       /* Optional logical database name */ 
  29.   DB_BTREE,   /* Database access method */ 
  30.   flags,      /* Open flags */ 
  31.   0);         /* File mode (using defaults) */ 
  32.  if (ret != 0) { 
  33.   /* Error handling goes here */ 
  34.  } 
  35.  
  36.  if( dbp != NULL) 
  37.   dbp->close(dbp,0); 
  38.  
  39.  return 0; 


B、用C++ API:

  1. #include "stdafx.h" 
  2. #include <db_cxx.h> 
  3.  
  4. int _tmain(int argc,_TCHAR* argv[]) 
  5.  Db db(NULL, 0);               // Instantiate the Db object 
  6.  
  7.  u_int32_t oFlags = DB_CREATE; // Open flags; 
  8.  
  9.  try { 
  10.   // Open the database 
  11.   db.open(NULL,                // Transaction pointer 
  12.    "my_db2.db",          // Database file name 
  13.    NULL,                // Optional logical database name 
  14.    DB_BTREE,            // Database access method 
  15.    oFlags,              // Open flags 
  16.    0);                  // File mode (using defaults) 
  17.   // DbException is not subclassed from std::exception, so 
  18.   // need to catch both of these. 
  19.  } catch(DbException &e) { 
  20.   // Error handling code goes here   
  21.  } catch(std::exception &e) { 
  22.   // Error handling code goes here 
  23.  } 
  24.  
  25.  try { 
  26.   // Close the database 
  27.   db.close(0); 
  28.   // DbException is not subclassed from std::exception, so 
  29.   // need to catch both of these. 
  30.  } catch(DbException &e) { 
  31.   // Error handling code goes here   
  32.  } catch(std::exception &e) { 
  33.   // Error handling code goes here 
  34.  } 


⑤、进一步学习:
 目前手头上唯一的资料是:D:\Program Files\Sleepycat Software\Berkeley DB 4.4.20\docs下的那些帮助文档;我想这应当也是最好的学习资料了。
 

被阅1087次, 0投一票
  • 看完了要说点啥么?
  • 昵称 (不填说不了话)
  • 信箱地址 (不会被公开,但是不填也说不了话)
  • 网址 (这个不填也成)
Powered by MiniBoke v2.0.0.8 Build 0828

Copyright © 2008 开源吧!. All rights reserved.

粤ICP备07500939号