home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Total C++ 2
/
TOTALCTWO.iso
/
borland
/
32snipit.pak
/
RECUPDAT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1997-05-06
|
5KB
|
152 lines
// BDE32 3.x - (C) Copyright 1996 by Borland International
// recupdat.c
#include "snipit.h"
static const char szTblName[] = "contacts";
static const char szTblType[] = szPARADOX;
//=====================================================================
// Function:
// RecordUpdate();
//
// Description:
// This example shows how to update records in a table.
//=====================================================================
void
RecordUpdate(void)
{
DBIResult rslt; // Value returned from IDAPI functions
hDBIDb hDb; // Handle to the database
hDBICur hCur; // Handle to the table
pBYTE pRecBuf; // Pointer to the record buffer
CURProps curProps; // Properties of the table
CHAR szLastName[20]; // Last Name
CHAR szFirstName[20]; // First Name
CHAR szCompany[30]; // Company Name
CHAR szPhone[16]; // Phone Number
Screen("*** Updating Records ***\r\n");
BREAK_IN_DEBUGGER();
Screen(" Initializing IDAPI...\r\n");
if (InitAndConnect(&hDb) != DBIERR_NONE)
{
Screen("\r\n*** End of Example ***");
return;
}
Screen(" Setting the database directory...");
rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
ChkRslt(rslt, "SetDirectory");
Screen(" Opening the \"%s\" table...", szTblName);
rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType,
NULL, NULL, NULL, dbiREADWRITE, dbiOPENSHARED,
xltFIELD, FALSE, NULL, &hCur);
if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
{
CloseDbAndExit(&hDb);
Screen("\r\n*** End of Example ***");
return;
}
// Add a record.
Screen(" \r\n Determine the size of the record and allocate a"
" buffer to hold records...");
// Allocate memory for the record buffer
rslt = DbiGetCursorProps(hCur, &curProps);
ChkRslt(rslt, "GetCursorProps");
pRecBuf = (pBYTE) malloc(curProps.iRecBufSize);
if (pRecBuf == NULL)
{
Screen(" Error - Out of memory");
rslt = DbiCloseCursor(&hCur);
ChkRslt(rslt, "CloseCursor");
CloseDbAndExit(&hDb);
Screen("\r\n*** End of Example ***");
return;
}
// Set the data to add.
strcpy(szLastName, "Smith");
strcpy(szFirstName, "John");
strcpy(szCompany, "Kauai Dive Shoppe");
strcpy(szPhone, "(304) 113-2244");
Screen(" Initialize the record buffer...");
rslt = DbiInitRecord(hCur, pRecBuf);
ChkRslt(rslt, "InitRecord");
Screen(" Add the fields to the record buffer...");
rslt = DbiPutField(hCur, 1, pRecBuf, (pBYTE) szLastName);
ChkRslt(rslt, "PutField");
rslt = DbiPutField(hCur, 2, pRecBuf, (pBYTE) szFirstName);
ChkRslt(rslt, "PutField");
rslt = DbiPutField(hCur, 3, pRecBuf, (pBYTE) szCompany);
ChkRslt(rslt, "PutField");
rslt = DbiPutField(hCur, 4, pRecBuf, (pBYTE) szPhone);
ChkRslt(rslt, "PutField");
Screen(" Add the record to the table...\r\n");
rslt = DbiInsertRecord(hCur, dbiNOLOCK, pRecBuf);
if (ChkRslt(rslt, "InsertRecord") != DBIERR_NONE)
{
if (rslt == DBIERR_KEYVIOL)
{
Screen(" Expected error - Continue...");
}
else
{
rslt = DbiCloseCursor(&hCur);
ChkRslt(rslt, "CloseCursor");
CloseDbAndExit(&hDb);
Screen("\r\n*** End of Example ***");
return;
}
}
Screen(" Search for the value in the table...\r\n");
rslt = DbiSetToKey(hCur, keySEARCHEQ, FALSE, 0, 0, pRecBuf);
ChkRslt(rslt, "SetToKey");
Screen(" Change the phone number of the just added record...\r\n");
Screen(" Get the record from the table...");
rslt = DbiGetNextRecord(hCur, dbiWRITELOCK, pRecBuf, NULL);
ChkRslt(rslt, "GetNextRecord");
Screen(" Change the value in the \"Phone\" field...");
strcpy(szPhone, "(304) 222-9876");
rslt = DbiPutField(hCur, 4, pRecBuf, (pBYTE) szPhone);
ChkRslt(rslt, "PutField");
Screen(" Write the changes to the table...");
rslt = DbiModifyRecord(hCur, pRecBuf, TRUE);
ChkRslt(rslt, "ModifyRecord");
Screen("\r\n Delete the record which was added...");
rslt = DbiDeleteRecord(hCur, NULL);
ChkRslt(rslt, "DeleteRecord");
free(pRecBuf);
Screen(" Close the %s table...", szTblName);
rslt = DbiCloseCursor(&hCur);
ChkRslt(rslt, "CloseCursor");
Screen(" Close the database and exit IDAPI...");
CloseDbAndExit(&hDb);
Screen("\r\n*** End of Example ***");
}