// BDE - (C) Copyright 1995 by Borland International // TextImp.c #include "snipit.h" #define NAMELEN 10 // Length of the name fields. #define PLACELEN 20 // Length of the POB field. #define DATELEN 11 // Length that a date will be displayed: mm\dd\yyyy. static const char szDBTblName[] = "DBPeople"; static const char szDBTblType[] = szDBASE; static const char szTXTTblName[] = "People"; static const char szTXTTblType[] = szASCII; // Field descriptor used in creating a table. static SNIPFAR FLDDesc DBfldDesc[] = { { // Field 1 - First name 1, // Field number "Name", // Field name fldZSTRING, // Field type fldUNKNOWN, // Field subtype NAMELEN, // Field size (1 or 0, except // BLOB or CHAR field) 0, // Decimal places ( 0 ) // computed 0, // Offset in record ( 0 ) 0, // Length in bytes ( 0 ) 0, // For NULL bits ( 0 ) fldvNOCHECKS, // Validity checks ( 0 ) fldrREADWRITE // Rights }, { // Field 2 - Place of Birth 2, "POB", fldZSTRING, fldUNKNOWN, PLACELEN, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE }, { // Field 3 - Date of Birth 3, "DOB", fldZSTRING, fldUNKNOWN, DATELEN, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE } }; // Array of field descriptors. static DBIResult AddRecordToTXT(phDBICur phCur, pCHAR pszName, pCHAR pszPOB, pCHAR pszDate); static DBIResult CreateImportDBTable(phDBIDb phDb); //===================================================================== // Function: // TextImport(); // // Description: // This example shows how to import data from a text table // to a dBASE table. This example will create two tables, one // text, one dBASE. Records will be added to the .TXT table // using DbiInsertRecord. The records will then be copied from // the text table to the dBASE table using the DbiBatchMove // function. //===================================================================== void TextImport (void) { DBIResult rslt; // Return value from IDAPI functions hDBIDb hDb; // Handle to the database hDBICur DBhCur; // dBASE cursor handle hDBICur TXThCur; // Text cursor handle CRTblDesc crTblDsc; // Table descriptor pFLDDesc pTXTFlds; // List of text fields BOOL bOverWrite = TRUE; // Overwrite, yes/no flag unsigned uNumFields; // Number of fields in the table Screen("*** Text Import Example ***\r\n"); BREAK_IN_DEBUGGER(); Screen(" Initializing IDAPI..."); 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(" Create a dBASE table..."); if (CreateImportDBTable(&hDb) != DBIERR_NONE) { Screen(" Clean up IDAPI..."); CloseDbAndExit(&hDb); Screen("\r\n*** End of Example ***"); return; } Screen(" Open the %s dBASE table...", szDBTblName); rslt = DbiOpenTable(hDb, (pCHAR) szDBTblName, (pCHAR) szDBTblType, NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED, xltFIELD, FALSE, NULL, &DBhCur); if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE) { CloseDbAndExit(&hDb); Screen("\r\n*** End of Example ***"); return; } // Initialize the create table descriptor. memset(&crTblDsc, 0, sizeof(CRTblDesc)); strcpy(crTblDsc.szTblName, szTXTTblName); strcpy(crTblDsc.szTblType, szTXTTblType); Screen(" Create a Text table..."); rslt = DbiCreateTable(hDb, bOverWrite, &crTblDsc); if (ChkRslt(rslt, "CreateTable") != DBIERR_NONE) { rslt = DbiCloseCursor(&DBhCur); ChkRslt(rslt, "CloseCursor"); rslt = DbiDeleteTable(hDb,(pCHAR) szDBTblName, (pCHAR) szDBTblType); ChkRslt(rslt, "DeleteTable"); CloseDbAndExit(&hDb); Screen("\r\n*** End of Example ***"); return; } Screen(" Open the %s text table...", szTXTTblName); rslt = DbiOpenTable(hDb, (pCHAR) szTXTTblName, "ASCIIDRV", NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED, xltFIELD, FALSE, NULL, &TXThCur); if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE) { rslt = DbiCloseCursor(&DBhCur); ChkRslt(rslt, "CloseCursor"); rslt = DbiDeleteTable(hDb,(pCHAR) szDBTblName, (pCHAR) szDBTblType); ChkRslt(rslt, "DeleteTable"); CloseDbAndExit(&hDb); Screen("\r\n*** End of Example ***"); return; } // The number of fields in the table. uNumFields = sizeof(DBfldDesc) / sizeof (DBfldDesc[0]); // Allocate memory for field descriptors. pTXTFlds = (pFLDDesc) malloc(uNumFields * sizeof(FLDDesc)); if (pTXTFlds == NULL) { rslt = DbiCloseCursor(&DBhCur); ChkRslt(rslt, "CloseCursor"); rslt = DbiCloseCursor(&TXThCur); ChkRslt(rslt, "CloseCursor"); rslt = DbiDeleteTable(hDb,(pCHAR) szDBTblName, (pCHAR) szDBTblType); ChkRslt(rslt, "DeleteTable"); CloseDbAndExit(&hDb); Screen("\r\n*** End of Example ***"); return; } // Convert the record structure to the text table format. rslt = DbiTranslateRecordStructure(NULL, uNumFields, DBfldDesc, (pCHAR) szTXTTblType, NULL, pTXTFlds); ChkRslt(rslt, "TranslateRecordStruct"); // Set the field map on the Text table. rslt = DbiSetFieldMap(TXThCur, uNumFields, pTXTFlds); if (ChkRslt(rslt, "SetFieldMap") != DBIERR_NONE) { rslt = DbiCloseCursor(&DBhCur); ChkRslt(rslt, "CloseCursor"); rslt = DbiCloseCursor(&TXThCur); ChkRslt(rslt, "CloseCursor"); rslt = DbiDeleteTable(hDb,(pCHAR) szDBTblName, (pCHAR) szDBTblType); ChkRslt(rslt, "DeleteTable"); CloseDbAndExit(&hDb); Screen("\r\n*** End of Example ***"); return; } Screen(" Add records to the text table..."); rslt = AddRecordToTXT(&TXThCur, "Jeffery", "1/28/1967", "San Jose" ); rslt = AddRecordToTXT(&TXThCur, "William", "8/12/1970", "New York"); rslt = AddRecordToTXT(&TXThCur, "Jimmy", "3/10/1924", "San Francisco"); rslt = AddRecordToTXT(&TXThCur, "Larry", "12/22/1933", "Germany"); rslt = DbiSetToBegin(DBhCur); ChkRslt(rslt, "SetToBegin"); rslt = DbiSetToBegin(TXThCur); ChkRslt(rslt, "SetToBegin"); // Copy the table. Screen(" Import the records to the dBASE table..."); rslt = DbiBatchMove(NULL, TXThCur, NULL, DBhCur, batAPPEND, 0, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, FALSE, FALSE, NULL, TRUE); ChkRslt(rslt, "Batch Move"); rslt = DbiSetToBegin(DBhCur); ChkRslt(rslt, "SetToBegin"); Screen("\r\n Displaying the updated dBASE table..."); DisplayTable(DBhCur, 0); // Clean up. free(pTXTFlds); Screen("\r\n Close the dBASE table..."); rslt = DbiCloseCursor(&DBhCur); ChkRslt(rslt, "CloseCursor"); Screen(" Delete the dBASE table..."); rslt = DbiDeleteTable(hDb,(pCHAR) szDBTblName, (pCHAR) szDBTblType); ChkRslt(rslt, "DeleteTable"); Screen(" Close the TEXT table..."); rslt = DbiCloseCursor(&TXThCur); ChkRslt(rslt, "CloseCursor"); Screen(" Close the database and exit IDAPI..."); CloseDbAndExit(&hDb); Screen("\r\n*** End of Example ***"); } //===================================================================== // Function: // AddRecordToTXT(phCur, pszName, pszPOB, pszDate); // // Input: phCur - Pointer to the cursor handle // pszName - Name // pszPOB - Place of birth // pszDate - Date // // Return: Result of adding the record to the table // // Description: // This function will add a record to the given TEXT table. //===================================================================== DBIResult AddRecordToTXT (phDBICur phCur, pCHAR pszName, pCHAR pszPOB, pCHAR pszDate) { DBIResult rslt; // Return value from IDAPI functions CURProps TblProps; // Table properties pBYTE pRecBuf; // Record buffer // Allocate a record buffer. rslt = DbiGetCursorProps(*phCur, &TblProps); ChkRslt(rslt, "GetProps"); pRecBuf = (pBYTE) malloc(TblProps.iRecBufSize * sizeof(BYTE)); if (pRecBuf == NULL) { Screen("Out of memory"); return DBIERR_NOMEMORY; } // Make sure we're starting with a clean record buffer. rslt = DbiInitRecord(*phCur, pRecBuf); ChkRslt(rslt, "InitRecord"); // Name. rslt = DbiPutField(*phCur, 1, pRecBuf, (pBYTE) pszName); ChkRslt(rslt, "PutField"); // Date of birth. rslt = DbiPutField(*phCur, 2, pRecBuf, (pBYTE) pszDate); ChkRslt(rslt, "PutField"); // Place fo Birth. rslt = DbiPutField(*phCur, 3, pRecBuf, (pBYTE) pszPOB); ChkRslt(rslt, "PutField"); rslt = DbiInsertRecord(*phCur, dbiNOLOCK, pRecBuf); ChkRslt(rslt, "InsertRec"); free(pRecBuf); return rslt; } //===================================================================== // Function: // CreateImportDBTable(phDb); // // Input: phDb - Pointer to the database handle // // Return: Result of the table initialization // // Description: // This function creates a dBASE table. //===================================================================== DBIResult CreateImportDBTable (phDBIDb phDb) { DBIResult rslt; // Value returned from IDAPI functions CRTblDesc crTblDsc; // Table descriptor BOOL bOverWrite = TRUE; // Overwrite, yes/no flag // The number of fields in the table. const unsigned uNumFields = sizeof(DBfldDesc) / sizeof (DBfldDesc[0]); // Initialize the create table descriptor. memset(&crTblDsc, 0, sizeof(CRTblDesc)); strcpy(crTblDsc.szTblName, szDBTblName); strcpy(crTblDsc.szTblType, szDBTblType); crTblDsc.iFldCount = uNumFields; crTblDsc.pfldDesc = DBfldDesc; rslt = DbiCreateTable(*phDb, bOverWrite, &crTblDsc); if (ChkRslt(rslt, "CreateTable") != DBIERR_NONE) { return rslt; } return rslt; }