1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| int TestOCILib()
{
if(OCI_Initialize(err_handler, "C:\\oracle\\ora81\\bin", OCI_ENV_DEFAULT) == false)
{
return -1;
}
printf("OCILib initialized\n");
OCI_Connection* pConnection = OCI_ConnectionCreate(DATABASENAME, USER, PWD, OCI_SESSION_DEFAULT);
if(pConnection == nullptr)
{
return -1;
}
FILE* pFile = fopen(FILENAME, "wt");
if(pFile == nullptr)
{
return -1;
}
printf("Connected to database\n");
OCI_Statement* pStatement = OCI_StatementCreate(pConnection);
const bool bSetPrefetchResult = OCI_SetPrefetchSize(pStatement, PREFETCHSIZE);
if(bSetPrefetchResult == false)
{
return -1;
}
OCI_ExecuteStmt(pStatement, REQUEST);
printf("Request in progress...\n");
OCI_Resultset* pResultSet = OCI_GetResultset(pStatement);
bool bFetchOk = true;
while(bFetchOk == true)
{
bFetchOk = OCI_FetchNext(pResultSet) != 0;
if(bFetchOk == true)
{
unsigned int const uiColNb = OCI_GetColumnCount(pResultSet);
for(unsigned int uiColIndex = 1; uiColIndex <= uiColNb; uiColIndex++)
{
OCI_Column* pColumn = OCI_GetColumn(pResultSet, uiColIndex);
char szColName[100];
strcpy(szColName, OCI_GetColumnName(pColumn));
const char* szType = OCI_GetColumnSQLType(pColumn);
fprintf(pFile, "%s = ", szColName);
// varchar2
if(strcmp(szType, "VARCHAR2") == 0)
{
const char* szColValue = OCI_GetString(pResultSet, uiColIndex);
fprintf(pFile, "%s", szColValue == nullptr ? "NULL" : szColValue);
}
// float
else if(strcmp(szType, "FLOAT") == 0)
{
const float fValue = (float)OCI_GetDouble(pResultSet, uiColIndex);
fprintf(pFile, "%f", fValue);
}
// integer/number
else if( strcmp(szType, "INTEGER") == 0
|| strcmp(szType, "NUMBER") == 0)
{
const int iValue = (int)OCI_GetBigInt(pResultSet, uiColIndex);
fprintf(pFile, "%d", iValue);
}
fprintf(pFile, "\n");
}
fprintf(pFile, "\n");
}
}
OCI_ReleaseResultsets(pStatement);
OCI_FreeStatement(pStatement);
OCI_FreeConnection(pConnection);
fclose(pFile);
printf("\n\nDone!");
return 0;
} |
Partager