/* xb_ex_v4_upd_dbf.cpp XBase64 Software Library Copyright (c) 1997,2003,2014,2022 Gary A Kunkel The xb64 software library is covered under the terms of the GPL Version 3, 2007 license. Email Contact: XDB-devel@lists.sourceforge.net XDB-users@lists.sourceforge.net This example demonstrates how to open the DBase III table created by xb_ex_v4_create_dbf and apply various updates to the table. */ #include using namespace xb; #undef XB_MEMO_FIELDS int main() { #ifdef XB_DBF4_SUPPORT xbInt16 fld_FIRSTNAME; xbInt16 fld_LASTNAME; xbInt16 fld_BIRTHDATE; xbInt16 fld_AMOUNT; xbInt16 fld_RETIRED; xbInt16 fld_ZIPCODE; xbInt16 fld_NUMFLD1; xbInt16 fld_NUMFLD2; #ifdef XB_MEMO_FIELDS xbInt16 fld_MEMO1; #endif /* define the classes */ xbXBase x; /* initialize xbase */ x.SetDataDirectory( PROJECT_DATA_DIR ); /* where all the tables live */ x.EnableMsgLogging(); x.WriteLogMessage( "Program [xb_ex_v4_upd_dbf] initializing..." ); xbDbf *MyTable = new xbDbf4( &x ); /* class for table */ #ifdef XB_INDEX_NDX xbNdx MyIndex1( &MyTable ); /* class for index 1 */ #endif xbInt16 iRc = 0; xbInt16 iErrorStop = 0; try{ if(( iRc = MyTable->Open( "MyV4Table1.DBF" )) != XB_NO_ERROR ){ iErrorStop = 1; throw iRc; } /* get the field numbers for all the fields in the table */ fld_FIRSTNAME = MyTable->GetFieldNo( "FIRSTNAME" ); fld_LASTNAME = MyTable->GetFieldNo( "LASTNAME" ); fld_BIRTHDATE = MyTable->GetFieldNo( "BIRTHDATE" ); fld_AMOUNT = MyTable->GetFieldNo( "AMOUNT" ); fld_RETIRED = MyTable->GetFieldNo( "RETIRED?" ); fld_ZIPCODE = MyTable->GetFieldNo( "ZIPCODE" ); fld_NUMFLD1 = MyTable->GetFieldNo( "NUMFLD1" ); fld_NUMFLD2 = MyTable->GetFieldNo( "NUMFLD2" ); #ifdef XB_MEMO_FIELDS fld_MEMO1 = MyTable->GetFieldNo( "MEMO1" ); #endif #ifdef XB_MDX_SUPPPORT std::cout << "Current tag = [" << MyTable->GetCurTagName() << "]\n"; #endif // Blank the record buffer if(( iRc = MyTable->BlankRecord()) != XB_NO_ERROR ){ iErrorStop = 7; throw iRc; } // put field examples - using field numbers if(( iRc = MyTable->PutField( fld_LASTNAME, "JONES" )) != XB_NO_ERROR ){ iErrorStop = 8; throw iRc; } if(( iRc = MyTable->PutField( fld_FIRSTNAME, "JERRY" )) != XB_NO_ERROR ){ iErrorStop = 9; throw iRc; } if(( iRc = MyTable->PutField( fld_AMOUNT, "12.35" )) != XB_NO_ERROR ){ iErrorStop = 10; throw iRc; } if(( iRc = MyTable->PutLogicalField( fld_RETIRED, "Y" )) != XB_NO_ERROR ){ iErrorStop = 11; throw iRc; } if(( iRc = MyTable->PutField( fld_BIRTHDATE, "19880209" )) != XB_NO_ERROR ){ iErrorStop = 12; throw iRc; } if(( iRc = MyTable->PutLongField( fld_ZIPCODE, 12345 )) != XB_NO_ERROR ){ iErrorStop = 13; throw iRc; } // Append the first record if(( iRc = MyTable->AppendRecord()) != XB_NO_ERROR ){ iErrorStop = 15; throw iRc; } // Blank the record buffer if(( iRc = MyTable->BlankRecord()) != XB_NO_ERROR ){ iErrorStop = 16; throw iRc; } // put field to the record buffer using field name (slightly less efficient than using field numbers) if(( iRc = MyTable->PutField( "LASTNAME", "FUCKPUTIN" )) != XB_NO_ERROR ){ iErrorStop = 20; throw iRc; } if(( iRc = MyTable->PutField( "FIRSTNAME", "ALBERT" )) != XB_NO_ERROR ){ iErrorStop = 21; throw iRc; } if(( iRc = MyTable->PutField( "AMOUNT", "987.55" )) != XB_NO_ERROR ){ iErrorStop = 22; throw iRc; } if(( iRc = MyTable->PutLogicalField( "RETIRED?", "N" )) != XB_NO_ERROR ){ iErrorStop = 23; throw iRc; } if(( iRc = MyTable->PutLongField( "ZIPCODE", 44256 )) != XB_NO_ERROR ){ iErrorStop = 24; throw iRc; } xbFloat f = (xbFloat) 12345.67; std::cout << f << std::endl; if(( iRc = MyTable->PutFloatField( fld_NUMFLD1, f )) != XB_NO_ERROR ){ iErrorStop = 13; throw iRc; } xbDouble d = 76543.21; if(( iRc = MyTable->PutDoubleField( fld_NUMFLD2, d )) != XB_NO_ERROR ){ iErrorStop = 14; throw iRc; } // Append the second record if(( iRc = MyTable->AppendRecord()) != XB_NO_ERROR ){ iErrorStop = 25; throw iRc; } // get a field with a field number xbString FirstName; if(( iRc = MyTable->GetField( fld_FIRSTNAME, FirstName )) < 0 ){ iErrorStop = 30; throw iRc; } std::cout << "First Name is [" << FirstName.Str() << "]" << std::endl; xbString LastName; if(( iRc = MyTable->GetField( "LASTNAME", LastName )) < 0 ){ iErrorStop = 31; throw iRc; } std::cout << "Last Name is [" << LastName.Str() << "]" << std::endl; xbInt16 iNoOfDecimals; if(( iRc = MyTable->GetFieldDecimal( "AMOUNT", iNoOfDecimals )) != XB_NO_ERROR ){ iErrorStop = 32; throw iRc; } std::cout << "There are " << iNoOfDecimals << " decimals in the AMOUNT field" << std::endl; xbString FieldName; if(( iRc = MyTable->GetFieldName( 4, FieldName )) != XB_NO_ERROR ){ iErrorStop = 36; throw iRc; } std::cout << "Field #4 name is " << FieldName.Str() << std::endl; xbString sRetired; if(( iRc = MyTable->GetLogicalField( "RETIRED?", sRetired )) < 0 ){ iErrorStop = 38; throw iRc; } std::cout << "Switch value = [" << sRetired.Str() << "]" << std::endl; xbInt32 lZip; if(( iRc = MyTable->GetLongField( "ZIPCODE", lZip )) < 0 ){ iErrorStop = 39; throw iRc; } std::cout << "Long value = [" << lZip << "]" << std::endl; if(( iRc = MyTable->GetFloatField( fld_NUMFLD1, f )) < 0 ){ iErrorStop = 41; throw iRc; } printf( "Field NUMFLD1 %8.2f\n", f ); if(( iRc = MyTable->GetDoubleField( fld_NUMFLD2, d )) < 0 ){ iErrorStop = 42; throw iRc; } printf( "Field NUMFLD2 %8.2f\n", d ); // Initialize the record buffer in preparation for another record if(( iRc = MyTable->BlankRecord()) != XB_NO_ERROR ){ iErrorStop = 44; throw iRc; } // Append another record (it will be blank) if(( iRc = MyTable->AppendRecord()) != XB_NO_ERROR ){ iErrorStop = 45; throw iRc; }; // mark current record for deletion if(( iRc = MyTable->DeleteRecord()) != XB_NO_ERROR ){ iErrorStop = 50; throw iRc; }; // save current record if(( iRc = MyTable->PutRecord()) != XB_NO_ERROR ){ iErrorStop = 51; throw iRc; } if(( iRc = MyTable->Commit()) != XB_NO_ERROR ){ iErrorStop = 52; throw iRc; } /* Close database and associated indexes */ if(( iRc = MyTable->Close()) != XB_NO_ERROR ){ iErrorStop = 53; throw iRc; } } catch( xbInt16 iRc ){ std::cout << "xb_ex_v4_upd_dbf error. Error stop point = [" << iErrorStop << "] iRc = [" << iRc << "]" << std::endl; std::cout << x.GetErrorMessage( iRc ) << std::endl; } #endif // XB_DBF4_SUPPORT return 0; }