summaryrefslogtreecommitdiff
path: root/src/examples/xb_ex_v4_upd_dbf.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/examples/xb_ex_v4_upd_dbf.cpp')
-rwxr-xr-xsrc/examples/xb_ex_v4_upd_dbf.cpp173
1 files changed, 88 insertions, 85 deletions
diff --git a/src/examples/xb_ex_v4_upd_dbf.cpp b/src/examples/xb_ex_v4_upd_dbf.cpp
index 6f68c73..96b6bdf 100755
--- a/src/examples/xb_ex_v4_upd_dbf.cpp
+++ b/src/examples/xb_ex_v4_upd_dbf.cpp
@@ -11,7 +11,7 @@ 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
+ This example demonstrates how to open the DBase IV table created by xb_ex_v4_create_dbf
and apply various updates to the table.
*/
@@ -20,27 +20,11 @@ Email Contact:
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 */
@@ -48,37 +32,30 @@ int main()
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
+ xbDbf *MyTable = new xbDbf4( &x ); /* class for DBase V4 table */
xbInt16 iRc = 0;
xbInt16 iErrorStop = 0;
try{
- if(( iRc = MyTable->Open( "MyV4Table1.DBF" )) != XB_NO_ERROR ){
- iErrorStop = 1;
+ if(( iRc = MyTable->Open( "Address.DBF" )) != XB_NO_ERROR ){
+ iErrorStop = 100;
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" );
+ xbInt16 iFld_FIRSTNAME = MyTable->GetFieldNo( "FIRSTNAME" );
+ xbInt16 iFld_LASTNAME = MyTable->GetFieldNo( "LASTNAME" );
+ xbInt16 iFld_BIRTHDATE = MyTable->GetFieldNo( "BIRTHDATE" );
+ xbInt16 iFld_AMOUNT1 = MyTable->GetFieldNo( "AMOUNT1" );
+ xbInt16 iFld_FRIEND = MyTable->GetFieldNo( "FRIEND?" );
+ xbInt16 iFld_ZIPCD = MyTable->GetFieldNo( "ZIPCD" );
+ xbInt16 iFld_AMOUNT2 = MyTable->GetFieldNo( "AMOUNT2" );
#ifdef XB_MEMO_FIELDS
- fld_MEMO1 = MyTable->GetFieldNo( "MEMO1" );
+ zbInt16 iFld_MEMO1 = MyTable->GetFieldNo( "MEMO1" );
#endif
@@ -88,187 +65,213 @@ int main()
// Blank the record buffer
if(( iRc = MyTable->BlankRecord()) != XB_NO_ERROR ){
- iErrorStop = 7;
+ iErrorStop = 110;
throw iRc;
}
// put field examples - using field numbers
- if(( iRc = MyTable->PutField( fld_LASTNAME, "JONES" )) != XB_NO_ERROR ){
- iErrorStop = 8;
+ if(( iRc = MyTable->PutField( iFld_LASTNAME, "JONES" )) != XB_NO_ERROR ){
+ iErrorStop = 120;
throw iRc;
}
+ // could also reference by field name (see below) but referencing by number
+ // is a little bit faster because it doesn't need to look up the number for the field name
+ // Alternative--> if(( iRc = MyTable->PutField( "LASTNAME", "JONES" )) != XB_NO_ERROR ){
- if(( iRc = MyTable->PutField( fld_FIRSTNAME, "JERRY" )) != XB_NO_ERROR ){
- iErrorStop = 9;
+ if(( iRc = MyTable->PutField( iFld_FIRSTNAME, "JERRY" )) != XB_NO_ERROR ){
+ iErrorStop = 130;
throw iRc;
}
- if(( iRc = MyTable->PutField( fld_AMOUNT, "12.35" )) != XB_NO_ERROR ){
- iErrorStop = 10;
+ if(( iRc = MyTable->PutField( iFld_AMOUNT1, "12.35" )) != XB_NO_ERROR ){
+ iErrorStop = 140;
throw iRc;
}
- if(( iRc = MyTable->PutLogicalField( fld_RETIRED, "Y" )) != XB_NO_ERROR ){
- iErrorStop = 11;
+ if(( iRc = MyTable->PutLogicalField( iFld_FRIEND, "Y" )) != XB_NO_ERROR ){
+ iErrorStop = 150;
throw iRc;
}
- if(( iRc = MyTable->PutField( fld_BIRTHDATE, "19880209" )) != XB_NO_ERROR ){
- iErrorStop = 12;
+ if(( iRc = MyTable->PutField( iFld_BIRTHDATE, "19880209" )) != XB_NO_ERROR ){
+ iErrorStop = 160;
throw iRc;
}
- if(( iRc = MyTable->PutLongField( fld_ZIPCODE, 12345 )) != XB_NO_ERROR ){
- iErrorStop = 13;
+ if(( iRc = MyTable->PutLongField( iFld_ZIPCD, 12345 )) != XB_NO_ERROR ){
+ iErrorStop = 170;
throw iRc;
}
// Append the first record
if(( iRc = MyTable->AppendRecord()) != XB_NO_ERROR ){
- iErrorStop = 15;
+ iErrorStop = 180;
+ throw iRc;
+ }
+
+ // Commit the updates
+ if(( iRc = MyTable->Commit()) != XB_NO_ERROR ){
+ iErrorStop = 190;
throw iRc;
}
// Blank the record buffer
if(( iRc = MyTable->BlankRecord()) != XB_NO_ERROR ){
- iErrorStop = 16;
+ iErrorStop = 200;
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;
+ iErrorStop = 210;
throw iRc;
}
if(( iRc = MyTable->PutField( "FIRSTNAME", "ALBERT" )) != XB_NO_ERROR ){
- iErrorStop = 21;
+ iErrorStop = 220;
throw iRc;
}
- if(( iRc = MyTable->PutField( "AMOUNT", "987.55" )) != XB_NO_ERROR ){
- iErrorStop = 22;
+ if(( iRc = MyTable->PutDoubleField( "AMOUNT1", (xbDouble) 987.55 )) != XB_NO_ERROR ){
+ iErrorStop = 230;
throw iRc;
}
- if(( iRc = MyTable->PutLogicalField( "RETIRED?", "N" )) != XB_NO_ERROR ){
- iErrorStop = 23;
+ if(( iRc = MyTable->PutLogicalField( "FRIEND?", "N" )) != XB_NO_ERROR ){
+ iErrorStop = 240;
throw iRc;
}
- if(( iRc = MyTable->PutLongField( "ZIPCODE", 44256 )) != XB_NO_ERROR ){
- iErrorStop = 24;
+ if(( iRc = MyTable->PutLongField( "ZIPCD", 44256 )) != XB_NO_ERROR ){
+ iErrorStop = 250;
throw iRc;
}
xbFloat f = (xbFloat) 12345.67;
std::cout << f << std::endl;
- if(( iRc = MyTable->PutFloatField( fld_NUMFLD1, f )) != XB_NO_ERROR ){
- iErrorStop = 13;
+ if(( iRc = MyTable->PutFloatField( iFld_AMOUNT2, f )) != XB_NO_ERROR ){
+ iErrorStop = 260;
throw iRc;
}
xbDouble d = 76543.21;
- if(( iRc = MyTable->PutDoubleField( fld_NUMFLD2, d )) != XB_NO_ERROR ){
- iErrorStop = 14;
+ if(( iRc = MyTable->PutDoubleField( iFld_AMOUNT1, d )) != XB_NO_ERROR ){
+ iErrorStop = 270;
throw iRc;
}
// Append the second record
if(( iRc = MyTable->AppendRecord()) != XB_NO_ERROR ){
- iErrorStop = 25;
+ iErrorStop = 280;
+ throw iRc;
+ }
+
+ // Commit the updates
+ if(( iRc = MyTable->Commit()) != XB_NO_ERROR ){
+ iErrorStop = 290;
throw iRc;
}
+
// get a field with a field number
xbString FirstName;
- if(( iRc = MyTable->GetField( fld_FIRSTNAME, FirstName )) < 0 ){
- iErrorStop = 30;
+ if(( iRc = MyTable->GetField( iFld_FIRSTNAME, FirstName )) < 0 ){
+ iErrorStop = 300;
throw iRc;
}
std::cout << "First Name is [" << FirstName.Str() << "]" << std::endl;
xbString LastName;
if(( iRc = MyTable->GetField( "LASTNAME", LastName )) < 0 ){
- iErrorStop = 31;
+ iErrorStop = 310;
throw iRc;
}
std::cout << "Last Name is [" << LastName.Str() << "]" << std::endl;
xbInt16 iNoOfDecimals;
- if(( iRc = MyTable->GetFieldDecimal( "AMOUNT", iNoOfDecimals )) != XB_NO_ERROR ){
- iErrorStop = 32;
+ if(( iRc = MyTable->GetFieldDecimal( "AMOUNT2", iNoOfDecimals )) != XB_NO_ERROR ){
+ iErrorStop = 320;
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;
+ iErrorStop = 330;
throw iRc;
}
std::cout << "Field #4 name is " << FieldName.Str() << std::endl;
- xbString sRetired;
- if(( iRc = MyTable->GetLogicalField( "RETIRED?", sRetired )) < 0 ){
- iErrorStop = 38;
+ xbString sFriend;
+ if(( iRc = MyTable->GetLogicalField( "FRIEND?", sFriend )) < 0 ){
+ iErrorStop = 340;
throw iRc;
}
- std::cout << "Switch value = [" << sRetired.Str() << "]" << std::endl;
+ std::cout << "Switch value = [" << sFriend.Str() << "]" << std::endl;
xbInt32 lZip;
if(( iRc = MyTable->GetLongField( "ZIPCODE", lZip )) < 0 ){
- iErrorStop = 39;
+ iErrorStop = 350;
throw iRc;
}
std::cout << "Long value = [" << lZip << "]" << std::endl;
- if(( iRc = MyTable->GetFloatField( fld_NUMFLD1, f )) < 0 ){
- iErrorStop = 41;
+ if(( iRc = MyTable->GetFloatField( iFld_AMOUNT2, f )) < 0 ){
+ iErrorStop = 360;
throw iRc;
}
printf( "Field NUMFLD1 %8.2f\n", f );
- if(( iRc = MyTable->GetDoubleField( fld_NUMFLD2, d )) < 0 ){
- iErrorStop = 42;
+ if(( iRc = MyTable->GetDoubleField( iFld_AMOUNT1, d )) < 0 ){
+ iErrorStop = 370;
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;
+ iErrorStop = 380;
throw iRc;
}
// Append another record (it will be blank)
if(( iRc = MyTable->AppendRecord()) != XB_NO_ERROR ){
- iErrorStop = 45;
+ iErrorStop = 390;
throw iRc;
};
// mark current record for deletion
if(( iRc = MyTable->DeleteRecord()) != XB_NO_ERROR ){
- iErrorStop = 50;
+ iErrorStop = 400;
throw iRc;
};
// save current record
if(( iRc = MyTable->PutRecord()) != XB_NO_ERROR ){
- iErrorStop = 51;
+ iErrorStop = 410;
throw iRc;
}
if(( iRc = MyTable->Commit()) != XB_NO_ERROR ){
- iErrorStop = 52;
+ iErrorStop = 420;
throw iRc;
}
+
+ // example code to loop through the table
+ for( xbUInt32 ulTuple = 1; ulTuple <= MyTable->GetRecordCount(); ulTuple++ ){
+ if(( iRc = MyTable->GetRecord( ulTuple ) != XB_NO_ERROR )){
+ iErrorStop = 430;
+ throw iRc;
+ }
+ // do something with the record here
+ std::cout << "Tuple = " << MyTable->GetCurRecNo() << std::endl;
+ }
+
/* Close database and associated indexes */
if(( iRc = MyTable->Close()) != XB_NO_ERROR ){
- iErrorStop = 53;
+ iErrorStop = 440;
throw iRc;
}