#include "oci.h" #include<stdio.h> #include<stdlib.h> #include<strings.h>
typedef struct { int r1; char r2[10];
}cdr;
int main() { OCIEnv *m_envhp; OCIError *m_errhp; OCIServer *m_srvhp; OCISvcCtx *m_svchp; OCIStmt *m_stmthp; sword swResult; cdr t_cdr; OCIDefine *hDefine = (OCIDefine *) 0; OCIDefine *hDefine1 = (OCIDefine *) 0; OCIDefine *hDefine2 = (OCIDefine *) 0; OCIDefine *hDefine3 = (OCIDefine *) 0; OCIDefine *hDefine4 = (OCIDefine *) 0; int i; char username[255]; char password[30]; char dbname[30]; char szSqlStr[255]; strcpy(username,"nari"); strcpy(password,"nariacc"); strcpy(dbname,"oraData"); bzero(&t_cdr, sizeof(t_cdr)); OCIInitialize((ub4)OCI_DEFAULT,(dvoid *)0,(dvoid *(*)(dvoid *,size_t))0,(dvoid *(*)(dvoid *,dvoid *,size_t))0,(void (*)(dvoid *,dvoid *)) 0 ); OCIEnvInit((OCIEnv **)&m_envhp, OCI_DEFAULT, (size_t) 0,(dvoid **) 0 ); OCIHandleAlloc( (dvoid *)m_envhp, (dvoid **)&m_errhp,OCI_HTYPE_ERROR,(size_t) 0, (dvoid **) 0); OCIHandleAlloc( (dvoid *)m_envhp, (dvoid **)&m_srvhp,OCI_HTYPE_SERVER,(size_t) 0, (dvoid **) 0); OCIHandleAlloc( (dvoid *)m_envhp, (dvoid **)&m_svchp,OCI_HTYPE_SVCCTX,(size_t) 0, (dvoid **) 0); OCIHandleAlloc( (dvoid *)m_envhp, (dvoid **)&m_stmthp,OCI_HTYPE_STMT,(size_t) 0, (dvoid **)0); OCIServerAttach(m_srvhp, m_errhp,(text *)dbname, strlen(dbname),(ub4) OCI_DEFAULT); OCILogon(m_envhp,m_errhp,&m_svchp,(text *)username,strlen(username),(text *)password,strlen(password),(text *)dbname,strlen(dbname)); sprintf( szSqlStr,"%s", "SELECT * FROM test"); OCIStmtPrepare(m_stmthp, m_errhp, (text*)szSqlStr, (ub4)strlen(szSqlStr),(ub4) OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT); OCIDefineByPos(m_stmthp,&hDefine1,m_errhp,1,&t_cdr.r1,sizeof(t_cdr.r1), SQLT_INT, NULL, NULL, NULL, OCI_DEFAULT); OCIDefineByPos(m_stmthp,&hDefine2,m_errhp,2,&t_cdr.r2,sizeof(t_cdr.r2), SQLT_CHR, NULL, NULL, NULL, OCI_DEFAULT); OCIDefineArrayOfStruct(hDefine, m_errhp, sizeof(t_cdr), 0, 0, 0); OCIStmtExecute( m_svchp, m_stmthp, m_errhp, (ub4) 1, (ub4) 0,(OCISnapshot *) NULL,(OCISnapshot *) NULL, (ub4)OCI_STMT_SCROLLABLE_READONLY ); int rows_fetched; printf("r1:%d r2:%s\n", t_cdr.r1, t_cdr.r2); while ((swResult=OCIStmtFetch2(m_stmthp,m_errhp,1,OCI_FETCH_NEXT,1,OCI_DEFAULT))!=OCI_NO_DATA) { printf("r1:%d r2:%s\n", t_cdr.r1, t_cdr.r2); }
OCIAttrGet((CONST void *)m_stmthp,OCI_HTYPE_STMT,(void *)&rows_fetched,(ub4 *)sizeof(rows_fetched),OCI_ATTR_ROW_COUNT ,m_errhp); printf("\n总共记录数:%d\n",rows_fetched);
OCILogoff( m_svchp, m_errhp ); OCIServerDetach( m_srvhp, m_errhp, OCI_DEFAULT ); OCIHandleFree((dvoid *) m_stmthp, OCI_HTYPE_STMT); OCIHandleFree((dvoid *) m_svchp, OCI_HTYPE_SVCCTX); OCIHandleFree((dvoid *) m_srvhp, OCI_HTYPE_SERVER); OCIHandleFree((dvoid *) m_errhp, OCI_HTYPE_ERROR); return 0;
}
|