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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
#include "ocilib.h"
void error_handler(OCI_Error *err);
/* methode 1 */
OCI_Statement * select_data1(OCI_Connection *cn, char *sql);
void read_data1(OCI_Statement *st);
/* methode 2 */
OCI_Resultset * select_data2(OCI_Connection *cn, char *sql);
void read_data2(OCI_Resultset *st);
int main(void)
{
OCI_Connection *cn;
if (!OCI_Initialize(error_handler, NULL, OCI_ENV_DEFAULT))
return EXIT_FAILURE;
cn = OCI_ConnectionCreate("db", "usr", "pwd", OCI_SESSION_DEFAULT);
if (cn)
{
/* methode 1 */
read_data1(select_data1(cn, "select 'method 1' from dual"));
/* methode 2 */
read_data2(select_data2(cn, "select 'method 2' from dual"));
OCI_ConnectionFree(cn);
}
OCI_Cleanup();
return EXIT_SUCCESS;
}
void error_handler(OCI_Error *err)
{
int err_type = OCI_ErrorGetType(err);
const char *err_msg = OCI_ErrorGetString(err);
printf("** %s - %s\n", err_type == OCI_ERR_WARNING ? "Warning" : "Error", err_msg);
}
OCI_Statement * select_data1(OCI_Connection *cn, char *sql)
{
OCI_Statement *st = NULL;
st = OCI_StatementCreate(cn);
if (OCI_ExecuteStmt(st, sql) == FALSE)
{
OCI_StatementFree(st);
st = NULL;
}
return st;
}
void read_data1(OCI_Statement *st)
{
if (st)
{
OCI_Resultset *rs = OCI_GetResultset(st);
while (OCI_FetchNext(rs))
{
printf("%s\n", OCI_GetString(rs, 1));
}
OCI_StatementFree(st);
}
}
OCI_Resultset * select_data2(OCI_Connection *cn, char *sql)
{
OCI_Statement *st = NULL;
OCI_Resultset *rs = NULL;
st = OCI_StatementCreate(cn);
OCI_SetFetchMode(st, OCI_SFM_SCROLLABLE);
if (OCI_ExecuteStmt(st, sql) == FALSE)
{
OCI_StatementFree(st);
st = NULL;
}
else
{
rs = OCI_GetResultset(st);
if (OCI_FetchNext(rs) == FALSE)
{
OCI_StatementFree(st);
st = NULL;
rs = NULL;
}
}
return rs;
}
void read_data2(OCI_Resultset *rs)
{
if (rs)
{
printf("%s\n", OCI_GetString(rs, 1));
while (OCI_FetchNext(rs))
{
printf("%s\n", OCI_GetString(rs, 1));
}
OCI_StatementFree(OCI_ResultsetGetStatement(rs));
}
} |
Partager