OCI1:
gcc -g -fsigned-char -DMITRACE_OFF -DIT_HAS_DISTINCT_LONG_DOUBLE -DIT_COMPILER_HAS_LONG_LONG -DIT_DO_NOT_SIMULATE_BOOL -D_INCLUDE_POSIX_SOURCE -D_PROTOTYPES -D_INCLUDE_XOPEN_SOURCE -D_H_LOCALEDEF -DODBC -g -I ../_incl -I/home/etl/dssprog/src/libftp/_incl -I/usr/local/include -I/home/etl/dssprog/src/LibsODBC -c -I/usr/local/include -I/usr/lib/oracle/xe/app/oracle/product/10.2.0/server/rdbms/public/ hh1.c
gcc -fsigned-char hh1.o -lm -lc -ldl -lpthread -lftp -L/home/etl/dssprog/lib -lsodbc -L/home/etl/dssprog/lib -lclntsh -lnsl -L/usr/lib/oracle/xe/app/oracle/product/10.2.0/server/lib -o hh1
OCI2:
gcc -g -fsigned-char -DMITRACE_OFF -DIT_HAS_DISTINCT_LONG_DOUBLE -DIT_COMPILER_HAS_LONG_LONG -DIT_DO_NOT_SIMULATE_BOOL -D_INCLUDE_POSIX_SOURCE -D_PROTOTYPES -D_INCLUDE_XOPEN_SOURCE -D_H_LOCALEDEF -DODBC -g -I ../_incl -I/home/etl/dssprog/src/libftp/_incl -I/usr/local/include -I/home/etl/dssprog/src/LibsODBC -c -I/usr/local/include -I/usr/lib/oracle/xe/app/oracle/product/10.2.0/server/rdbms/public/ cdemocp.c
gcc -fsigned-char cdemocp.o -lm -lc -ldl -lpthread -lftp -L/home/etl/dssprog/lib -lsodbc -L/home/etl/dssprog/lib -lclntsh -lnsl -L/usr/lib/oracle/xe/app/oracle/product/10.2.0/server/lib -o cdemocp
OCI1:
#include
#include
#include
#include
static OCIEnv *p_env;
static OCIError *p_err;
static OCISvcCtx *p_svc;
static OCIStmt *p_sql;
static OCIDefine *p_dfn = (OCIDefine *) 0;
static OCIBind *p_bnd = (OCIBind *) 0;
int main()
{
int p_bvi;
char *p_sli;
int rc;
char errbuf[100];
int errcode;
char mysql[100];
p_sli=(char *)malloc(20);
memset(p_sli,0,20);
/* Initialize OCI evironment*/
rc = OCIEnvCreate((OCIEnv **) &p_env,OCI_DEFAULT,(dvoid *)0,
(dvoid * (*)(dvoid *, size_t)) 0,
(dvoid * (*)(dvoid *, dvoid *, size_t))0,
(void (*)(dvoid *, dvoid *)) 0,
(size_t) 0, (dvoid **) 0);
/* Initialize handles */
rc = OCIHandleAlloc( (dvoid *) p_env, (dvoid **) &p_err, OCI_HTYPE_ERROR,
(size_t) 0, (dvoid **) 0);
rc = OCIHandleAlloc( (dvoid *) p_env, (dvoid **) &p_svc, OCI_HTYPE_SVCCTX,
(size_t) 0, (dvoid **) 0);
/* Connect to database server */
// rc = OCILogon(p_env, p_err, &p_svc, "etl", 2, "etl", 2, "biwg", 4);
// rc = OCILogon(p_env, p_err, &p_svc, "etl/etl@biwg", 10, NULL, -1, NULL, -1);
rc = OCILogon(p_env, p_err, &p_svc, "etl", (ub4) 3, "etl", (ub4) 3, "biwg", (ub4) 4);
if (rc != 0) {
OCIErrorGet((dvoid *)p_err, (ub4) 1, (text *) NULL, &errcode, errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR);
printf("Error - %.*sn", 512, errbuf);
exit(8);
}
else
{
printf("Connect to orcl successful!n");
}
/* Allocate and prepare SQL statement */
rc = OCIHandleAlloc((dvoid *) p_env, (dvoid **) &p_sql,
OCI_HTYPE_STMT, (size_t) 0, (dvoid **) 0);
/* set my sql statement */
strcpy(mysql,"select date_cd from t");
rc = OCIStmtPrepare(p_sql, p_err, mysql,
(ub4) strlen(mysql), (ub4) OCI_NTV_SYNTAX, (ub4) OCI_DEFAULT);
/* Bind the values for the bind variables */
p_bvi = 102; /* Use employee_id=102 */
rc = OCIBindByName(p_sql, &p_bnd, p_err, (text *) ":x",
-1, (dvoid *) &p_bvi, sizeof(int), SQLT_INT, (dvoid *) 0,
(ub2 *) 0, (ub2 *) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT);
/* Define the select list items */
rc = OCIDefineByPos(p_sql, &p_dfn, p_err, 1, (dvoid *) p_sli,
(sword) 20, SQLT_STR, (dvoid *) 0, (ub2 *)0,
(ub2 *)0, OCI_DEFAULT);
/* Execute the SQL statment */
rc = OCIStmtExecute(p_svc, p_sql, p_err, (ub4) 1, (ub4) 0,
(CONST OCISnapshot *) NULL, (OCISnapshot *) NULL, OCI_DEFAULT);
while (rc != OCI_NO_DATA) { /* Fetch the remaining data */
printf("%sn",p_sli);
rc = OCIStmtFetch(p_sql, p_err, 1, 0, 0);
}
rc = OCILogoff(p_svc, p_err); /* Disconnect */
rc = OCIHandleFree((dvoid *) p_sql, OCI_HTYPE_STMT); /* Free handles */
rc = OCIHandleFree((dvoid *) p_svc, OCI_HTYPE_SVCCTX);
rc = OCIHandleFree((dvoid *) p_err, OCI_HTYPE_ERROR);
return;
}
OCI2:
#include
#include
#include
#include
static void check_error(OCIError *errhp, sword status)
{
text errbuf[512];
sb4 errcode;
switch (status)
{
case OCI_SUCCESS:
break;
case OCI_SUCCESS_WITH_INFO:
fprintf(stderr, "Error - OCI_SUCCESS_WITH_INFOn");
break;
case OCI_NEED_DATA:
fprintf(stderr, "Error - OCI_NEED_DATAn");
break;
case OCI_NO_DATA:
fprintf(stderr, "Error - OCI_NO_DATAn");
break;
case OCI_ERROR:
errcode = 0;
OCIErrorGet((dword *)errhp, (ub4)1, (text *)NULL, &errcode,
errbuf, (ub4)sizeof(errbuf), OCI_HTYPE_ERROR);
fprintf(stderr, "Error - %sn", errbuf);
break;
case OCI_INVALID_HANDLE:
fprintf(stderr, "Error - OCI_INVALID_HANDLEn");
break;
case OCI_STILL_EXECUTING:
fprintf(stderr, "Error - OCI_STILL_EXECUTINGn");
break;
case OCI_CONTINUE:
fprintf(stderr, "Error - OCI_CONTINUEn");
break;
default:
fprintf(stderr, "Error - Unknown Error Coden");
break;
}
}
/* EXECUTE SELECT */
static void query_rows(OCIEnv *envhp, OCIError *errhp,
const OraText *username, const OraText *password, const OraText *database)
{
int i;
sword lstat, status;
text sqlstr[256];
float emp_sal;
ub4 emp_no, emp_dept;
text emp_name[10], emp_job[9];
OCIStmt *stmthp = (OCIStmt *)0;
OCISvcCtx *svchp = (OCISvcCtx *)0;
OCIDefine *defhp1 = (OCIDefine *)0,
*defhp2 = (OCIDefine *)0,
*defhp3 = (OCIDefine *)0,
*defhp4 = (OCIDefine *)0,
*defhp5 = (OCIDefine *)0;
/* Login Oracle */
lstat = OCILogon(envhp, errhp, &svchp,
(CONST OraText *)username, (ub4)strlen((const char *)username),
(CONST OraText *)password, (ub4)strlen((const char *)password),
(CONST OraText *)database, (ub4)strlen((const char *)database));
if (lstat)
{
check_error(errhp, lstat);
return;
}
sprintf(sqlstr, "SELECT empno, ename, job, sal, deptno FROM emp ORDER BY empno");
OCIHandleAlloc(envhp, (dvoid **)&stmthp, OCI_HTYPE_STMT,
(size_t)0, (dvoid **)0);
check_error(errhp, OCIStmtPrepare(stmthp, errhp,
(text *)sqlstr, (ub4)strlen((const char *)sqlstr), OCI_NTV_SYNTAX, OCI_DEFAULT));
check_error(errhp, OCIDefineByPos(stmthp, &defhp1, errhp, (ub4)1,
(dvoid *)&emp_no, (sb4)sizeof(emp_no), (ub2)SQLT_INT,
(dvoid *)0, (ub2 *)0, (ub2 *)0, OCI_DEFAULT));
check_error(errhp, OCIDefineByPos(stmthp, &defhp2, errhp, (ub4)2,
(dvoid *)&emp_name, (sb4)sizeof(emp_name), (ub2)SQLT_STR,
(dvoid *)0, (ub2 *)0, (ub2 *)0, OCI_DEFAULT));
check_error(errhp, OCIDefineByPos(stmthp, &defhp3, errhp, (ub4)3,
(dvoid *)&emp_job, (sb4)sizeof(emp_job), (ub2)SQLT_STR,
(dvoid *)0, (ub2 *)0, (ub2 *)0, OCI_DEFAULT));
check_error(errhp, OCIDefineByPos(stmthp, &defhp4, errhp, (ub4)4,
(dvoid *)&emp_sal, (sb4)sizeof(emp_sal), (ub2)SQLT_FLT,
(dvoid *)0, (ub2 *)0, (ub2 *)0, OCI_DEFAULT));
check_error(errhp, OCIDefineByPos(stmthp, &defhp5, errhp, (ub4)5,
(dvoid *)&emp_dept, (sb4)sizeof(emp_dept), (ub2)SQLT_INT,
(dvoid *)0, (ub2 *)0, (ub2 *)0, OCI_DEFAULT));
if (lstat = OCIStmtExecute(svchp, stmthp, errhp, (ub4)0,
(ub4)0, (OCISnapshot *)0, (OCISnapshot *)0, OCI_DEFAULT))
{
check_error(errhp, lstat);
return;
}
status = 0;
status = OCIStmtFetch(stmthp, errhp, 1, OCI_FETCH_NEXT, OCI_DEFAULT);
while (status != OCI_NO_DATA)
{
printf("emp_no: %u t emp_name: %s t job: %s tsalary: %7.2f tdept_no: %un",
emp_no, emp_name, emp_job, emp_sal, emp_dept);
status = OCIStmtFetch(stmthp, errhp, 1, OCI_FETCH_NEXT, OCI_DEFAULT);
}
check_error(errhp, OCIHandleFree((dvoid *)stmthp, OCI_HTYPE_STMT));
check_error(errhp, OCILogoff((dvoid *)svchp, errhp));
}
static void insert_rows(OCIEnv *envhp, OCIError *errhp,
const OraText *username, const OraText *password,
const OraText *database, text *sqlstr)
{
sword lstat;
OCISvcCtx *svchp = (OCISvcCtx *)0;
OCIStmt *stmthp = (OCIStmt *)0;
if (lstat = OCILogon(envhp, errhp, &svchp,
(CONST OraText *)username, (ub4)strlen((const char *)username),
(CONST OraText *)password, (ub4)strlen((const char *)password),
(CONST OraText *)database, (ub4)strlen((const char *)database)))
{
check_error(errhp, lstat);
return;
}
OCIHandleAlloc(envhp, (dvoid **)&stmthp, OCI_HTYPE_STMT,
(size_t)0, (dvoid **)0);
check_error(errhp, OCIStmtPrepare(stmthp, errhp,
(CONST OraText *)sqlstr, (ub4)strlen((const char *)sqlstr),
OCI_NTV_SYNTAX, OCI_DEFAULT));
check_error(errhp, OCIStmtExecute(svchp, stmthp, errhp,
(ub4)1, (ub4)0, (OCISnapshot *)0, (OCISnapshot *)0, OCI_DEFAULT));
check_error(errhp, OCITransCommit(svchp, errhp, (ub4)0));
check_error(errhp, OCIHandleFree((dvoid *)stmthp, OCI_HTYPE_STMT));
check_error(errhp, OCILogoff((dvoid *)svchp, errhp));
}
int main(int argc, char **argv)
{
CONST OraText *database = (OraText *)"";
CONST OraText *username = (OraText *)"scott";
CONST OraText *password = (OraText *)"tiger";
OCIEnv *envhp;
OCIError *errhp;
text sqlstr[256];
ub4 conMax, conIncr, conMin;
OCIEnvCreate(&envhp, OCI_THREADED, (dvoid *)0, NULL, NULL, NULL, 0, (dvoid **)0);
OCIHandleAlloc((dvoid *)envhp, (dvoid *)&errhp, OCI_HTYPE_ERROR,
(size_t)0, (dvoid **)0);
conMax = 3;
conMin = 1;
conIncr= 1;
sprintf(sqlstr, "INSERT INTO emp(empno, ename, job, sal, deptno) VALUES(30, 'Gan', 'DBA', 9000, 10)");
printf("nInsert values to the table: n%sn", sqlstr);
insert_rows(envhp, errhp, username, password, database, sqlstr);
printf("nDisplay the table information.n");
query_rows(envhp, errhp, username, password, database);
check_error(errhp, OCIHandleFree((dvoid *)errhp, OCI_HTYPE_ERROR));
return (0);
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/22630256/viewspace-1035446/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/22630256/viewspace-1035446/