int CBDB_PhoneBookDemo3::Run(void) { try { LoadPhoneBook(); SPhoneBookDB dbf; dbf.Open(s_PhoneBookDBFileName, CBDB_File::eReadOnly); BuildZipIndex(dbf); SPhoneBookZipIDX idx; idx.Open(s_PhoneBookZipIDXFileName, CBDB_File::eReadOnly); SearchPhoneBook(dbf, idx, 10785); } catch (CBDB_ErrnoException& ex) { cout << "Error! DBD errno exception:" << ex.what(); return 1; } catch (CBDB_LibException& ex) { cout << "Error! DBD library exception:" << ex.what(); return 1; } return 0; }
// // Function scans all records in the phone book table. // One of the features of the BDB library is that cursor is attached // to the main open datafile instance and when we call fetch data become // immediately available in the datafile structure fields. // Cursor only defines search criteria. // void PrintPhoneBook() { SPhoneBookDB dbf; dbf.Open(s_PhoneBookDBFileName, CBDB_File::eReadOnly); CBDB_FileCursor cur(dbf); cur.SetCondition(CBDB_FileCursor::eFirst); unsigned int recs_fetched = 0; while (cur.Fetch() == eBDB_Ok) { cout << "====================================" << endl; PrintRecord(dbf); ++recs_fetched; } cout << "====================================" << endl; cout << "Records found:" << recs_fetched << endl; }
void LoadPhoneBook() { SPhoneBookDB dbf; dbf.Open(s_PhoneBookDBFileName, CBDB_File::eCreate); dbf.person_id = 1; dbf.first_name = "John"; dbf.last_name = "Doe"; dbf.zip_code = 10785; dbf.phone = "+1(240)123456"; EBDB_ErrCode ret = dbf.Insert(); // Check Insert result, it can be 'eBDB_KeyDup' // if primary key already exists if (ret != eBDB_Ok) { cout << "Insert failed!" << endl; exit(1); } dbf.person_id = 2; dbf.first_name = "Antony"; dbf.last_name = "Smith"; dbf.zip_code = 10785; dbf.phone = "(240)234567"; ret = dbf.Insert(); if (ret != eBDB_Ok) { cout << "Insert failed!" << endl; exit(1); } dbf.person_id = 3; dbf.first_name = "John"; dbf.last_name = "Public"; dbf.zip_code = 40000; ret = dbf.Insert(); if (ret != eBDB_Ok) { cout << "Insert failed!" << endl; exit(1); } }