Berkeley DB How to use

xiaoxiao2021-03-06  47

Berkeley DB is an open source embedded database developed by Sleepcat. It has the advantages of lightweight and scalable. But it does not support SQL statements, and it is relatively complicated. The following is taught in the VC to store the data structure made in the VC. In Google Search Berkely DB Enter Sleepycat to download the ZIP installation package, compile. Establish engineering testberkeleydb, copy libdb43.dll and libdb43.lib to the current project, add libdb43.dll to the project's setting. Copy DB.H and DB_CXX.H to the current directory. Add ICQTYPES.H: #ifndef _icq_types_h_ # define _icq_types_h_

#include #include #include #include

Using namespace std;

Typedef unsigned char uint8; typef unsigned short uint16; typedef unsigned long uint32;

Typedef List strlist; typef list ptrlist; typedef list uinlist;

#ENDIF

Add Icqdb.h; #ifndef _icq_db_h # define _icq_db_h

#include "icqtypes.h" #include "db_cxx.h"

#define max_block_size 4096

Class DBOUTSTREAM {public: dboutstream () {cursor = data;} char * getdata () {return data;} int getSize () {return (CURSOR - DATA);

DBOutStream & operator << (uint8 b); DBOutStream & operator << (uint16 w); DBOutStream & operator << (uint32 dw); DBOutStream & operator << (const char * str); DBOutStream & operator << (const string & str) {return operator << (str.c_str ());} DBOUTSTREAM & OPERATOR << (Strlist & strlist); private: char data [max_block_size]; char * cursor;};

class DBInStream {public: DBInStream (void * d, int n) {cursor = data = (char *) d; datalen = n;} DBInStream & operator >> (uint8 & b); DBInStream & operator >> (uint16 & w); DBInStream & operator >> (UINT32 & DW); Dbinstream & Operator >> (String & Strlist);

PRIVATE: CHAR * DATA; CHAR * CURSOR; INT DATALEN;

Class DBSerialize {public: Virtual Void Save (DBOUTSTREAM & OUT) = 0; Virtual Void Load (DBINSTREAM & IN) = 0;};

#ENDIF Add Icqdb.cpp: #include "stdafx.h" #include "icqdb.h" #include "string.h" #include "icqtypes.h"

#define index_user 0 # Define index_options 0xffffffff # define index_group 0xffffffe

DBOUTSTREAM & DBOUTSTREAM :: Operator << (uint8 b) {if (Cursor <= DATA MAX_BLOCK_SIZE - SIZEOF (B)) * CURSOR = B; Return (* this);}

DBOUTSTREAM & DBOUTSTREAM :: Operator << (uint16 w) {if (Cursor <= DATA MAX_BLOCK_SIZE - SIZEOF (W)) {* (uint16 *) Cursor = W; Cursor = SizeOf (W);} Return (* this) }

DBOUTSTREAM & DBOUTSTREAM :: Operator << (uint32 dw) {if (CURSOR <= DATA MAX_BLOCK_SIZE - SIZEOF (DW)) {* (uint32 *) CURSOR = DW; CURSOR = SizeOf (DW);} return (* this) }

DBOUTSTREAM & DBOUTSTREAM :: Operator << (const char * str) {uint16 len = Strlen (STR) 1; if (Cursor <= DATA MAX_BLOCK_SIZE - SIZEOF (LEN) - LEN) {* this << Len; Memcpy (Cursor , Str, LEN); CURSOR = LEN;} Return (* this);

DBOUTSTREAM & DBOUTSTREAM :: Operator << (Strlist & strlist) {uint16 n = 0; char * OLD = Cursor; Cursor = SizeOf (n); strlist :: item i; for (i = strlist.begin (); i! = Strlist.end (); i ) {Operator << (* i); n ;} char * p = cursor; cursor = Old; operator << (n); cursor = p; return (* this);}

Dbinstream & DbinStream :: Operator >> (uint8 & b) {if (Cursor <= DATA DATALEN - SIZEOF (B)) B = * CURSOR ; ELSE B = 0; Return (* this);}

Dbinstream & dbinstream :: Operator >> (uint16 & w) {IF (Cursor <= DATA DATALEN - SIZEOF (W)) {w = * (uint16 *) CURSOR; CURSOR = SIZEOF (W);} else w = 0; Return (* this);} DBINSTREAM & DBINSTREAM :: Operator >> (UINT32 & DW) {IF (Cursor <= DATA DATALEN - SIZEOF (DW)) {dw = * (uint32 *) Cursor; Cursor = SizeOf (DW) } Else dw = 0; return (* this);}

Dbinstream & dbinstream :: Operator >> (String & Str) {uint16 len; operator >> (LEN);

IF (CURSOR <= DATA DATALEN - LEN &&! CURSOR [LEN - 1]) {str = cursor; cursor = len;} else str = ""; return (* this);

Dbinstream & Dbinstream :: Operator >> (Strlist & strlist) {uint16 num; operator >> (NUM); int N = (int) NUM;

Strlist.clear (); while (n-> 0) {string s; operator >> (s); strlist.push_back (s);} return (* this);}

Add CPERSON Class: CPERSON.H: // Person.h: interface for the cperson class.///

#if! defined (AFX_PERSON_H__D259A8B7_7ACB_4624_8C9E_394B11FFA5C9__INCLUDED _) # Define AFX_PERSON_H__D259A8B7_7ACB_4624_8C9E_394B11FFA5C9__INCLUDED_

#if_MSC_VER> 1000 # prgma overce # endif //_msc_ver> 1000 # include "icqdb.h"

class CPerson: public DBSerialize {public: CPerson (); virtual ~ CPerson (); public: string Name; uint16 age; string Describe; void save (DBOutStream & out); void load (DBInStream & in);}; # endif //! Defined (AFX_PERSON_H__D259A8B7_7ACB_4624_8C9E_394B11FFA5C9__INCLUDED_)

Person.cpp: // Person.cpp: Implementation of the cperson class.////

#include "stdafx.h" #include "testberkeleydb.h" #include "person.h"

#ifdef _debug # undef this_filestatic char this_file [] = __ file __; # define new debug_new # endif /// CONSTRUCTION / DESTRUCTION //

CPERSON :: CPERSON () {

}

CPERSON :: ~ CPERSON () {

} Void CPERSON :: Save (DBOUTSTREAM & OUT) {OUT << Name << Age << Describe;}

Void CPERSON :: Load (dbinstream & in) {in >> Name >> Age >> Describe;}

Below is the main function: // TestberkeleyDb.cpp: defines the entry point for the console application.//

#include "stdafx.h" #include "testberkeleydb.h" #include "db_cxx.h" #include "icqtypes.h" #include "icqdb.h" #include "person.h"

#ifdef _debug # define new debug_new # undef this_filestatic char this_file [] = __file __; # ENDIF

/// the one and only application objection Object

CWINAPPPP;

// using namespace std;

Bool Delpeople (char * filename, string index) {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 fileName, // 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 (dbexception & e) {// error handling code goes here} catch (std :: exception & e) {// error handling code goes here} dbc * cursorp; try {// Database Open OMITTED // Get The Cursor DB.CURSOR (NULL, & CURSORP, 0); // SET UP OUR DBTS DBT DATA; DBT Key; Key.set_Data ((void *) Index.c_str ()); Key .SET_SIZE (Index.Length () 1); // Iterage over the database, deleding each record in turn. int R; while ((Ret = Cursorp-> Get (& key, & data, db_set) == 0) { CURSORP-> DEL (0);}} catch (dbexception & e) {db.err (E.GET_ERR NO (), "error!");} catch (std :: exception & e) {db.errx ("error!% s", e.What ());} // Cursors Must Be Closed if (Cursorp! = NULL) CURSORP-> Close (); db.close (0); return true;}

Bool Delpeople (Char * filename, uint32 index) {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 fileName, // 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 (dbexception & e) {// error handling code goes here} catch (std :: exception & e) {// error handling code goes here} dbc * cursorp; try {// Database Open Omitted // Get The Cursor DB.CURSOR (NULL, & CURSORP, 0); // SET UP OUR DBTS DBT DATA; DBT Key; Key.Set_Data (& INDEX); Key.Set_Size // ipreate over the database, deleding each record in turn. Int R; while ((Ret = Cursorp-> Get) == 0) {cursorp-> del (0);}} catch (DBEXCEPTION & E) {db.err (E.GET_ERRNO (), "ERROR!");} C ATCH (std :: exception & e) {db.errx ("Error!% S", E.What ());} // Cursors Must Be Closed if (Cursorp! = NULL) CURSORP-> Close (); DB. Close (0); return true;

bool loadPeople (char * fileName, uint32 index, DBSerialize & obj) {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 fileName, // 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 {// error handling code goes here} catch (std :: exception & e) {// error handling code goes here} DBT Key , data; key.set_data (& index); key.set_size (SIZEOF (INDEX));

IF (DB.Get (NULL, & Key, & Data, 0)! = 0) {db.close (0); Return False;}

Dbinstream in (data.get_data (), data.get_size ()); Obj.Load (in);

db.close (0); return true;

bool loadPeople (char * fileName, string index, DBSerialize & obj) {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 fileName, // 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 {// error handling code goes here} catch (std :: exception& e) {// error handling code goes here}

DBT key, data; key.set_data ((void *) index.c_str ()); key.set_size (Index.length () 1);

IF (DB.Get (NULL, & Key, & Data, 0)! = 0) {db.close (0); Return False;}

Dbinstream in (data.get_data (), data.get_size ()); Obj.Load (in); db.close (0); return true;}

bool savePeople (char * fileName, uint32 index, DBSerialize & obj) {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 fileName, // 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} DBOUTSTREAM OUT Obj.save (out);

DBT Key, Data; Key.set_data (& INDEX); key.set_size; data.set_data (out.getdata ()); data.set_size (out.getsize ()); int R = db.put (NULL, & Key, & Data, 0); db.close (0); return (RET == 0);}

bool savePeople (char * fileName, string index, DBSerialize & obj) {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 fileName, // 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} DBOUTSTREAM OUT Obj.save (out);

DBT Key, Data; Key.set_Data ((void *) index.c_str ()); key.set_size (index.length () 1); data.set_data (out.getdata ()); data.set_size (Out. GetSize ()); int RET = db.put (null, & key, & data, 0); db.close (0); return (RET == 0);} Bool PrintAllPeople (Char * filename) {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 fileName, // 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} DBC * CURSORP; Try {// Database Open OMITTED // Get The Cursor DB.CURSOR (NULL , & cursorp, 0); // SET UP OUR DBTS DBT DATA; DBT Key; // Iteerate Over the Database, Deleting Each Record in Turn. INT Ret; While ((Ret = Cursorp-> Get) == 0) {// Cursorp-> Del (0); CPERSON P; Dbinstream in (Data.get_Data (), Data.get_size )); P.Load (in); Printf ("% s / n", p.describe);

}}}}} Catch (DBEXCEPTION & E) {db.err (E.GET_ERRNO (), "Error!");} Catch (std :: exception & e) {db.ERRX ("Error!% S", E.WHAT () } // Cursors Must Be Closed if (Cursorp! = NULL) CURSORP-> Close (); db.close (0); return true;}

INT _TMAIN (int Argc, tchar * argv [], tchar * envp []) {int nretcode = 0;

// Initialize MFC and Print and Error On Failure if (! AFXWininit (: getModuleHandle (Null), Null, :: getcommandline (), 0)) {// Todo: Change Error Code To Suit Your Needs CERR << _t "Fatal Error: MFC Initialization") << ENDL; NRETCODE = 1;} else {string index1 = "liang zhijian"; string index2 = "zou dan"; CPERSON P1; P1.AGE = 0; p1.name = " Liang zhijian "; p1.describe =" student1 "; SavePeople (" mydb.db ", index1, p1);

CPERSON P2; P2.AME = 1; p2.Name = "Zou Dan"; p2.describe = "student2"; SavePeople ("MyDb.db", INDEX2, P2);

CPERSON P3; LOADPEOPLE ("MyDb.db", "Liang Zhijian", P3); Printf ("% S / N", P3.Describe; PrintAllPeople ("mydb.db"); Delpeople ("mydb.db", "Liang zhijian"); PrintallPeople ("mydb.db");} return nretcode;}

转载请注明原文地址:https://www.9cbs.com/read-81362.html

New Post(0)