数据库
首页 > 数据库> > 通过oci.dll访问oracle数据库

通过oci.dll访问oracle数据库

作者:互联网

#include"windows.h"
#include"oci.h"
#include"stdio.h"

int main()
{
char buffer[100];

OCISession *authp = (OCISession *) 0;
OCIServer *srvhp;
OCISvcCtx *svchp;
OCIStmt *stmthp;
OCIEnv *envhp;
OCIError *errhp;
short r;
(void) OCIInitialize((ub4) OCI_DEFAULT, (dvoid )0,
(dvoid * (
)(dvoid , size_t)) 0,
(dvoid * (
)(dvoid *, dvoid , size_t))0,
(void (
)(dvoid *, dvoid *)) 0 );
(void) OCIEnvInit( (OCIEnv **) &envhp, OCI_DEFAULT, (size_t) 0,
(dvoid **) 0 );
(void) OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &errhp, OCI_HTYPE_ERROR,
(size_t) 0, (dvoid *) 0);
/
server contexts */
(void) OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &srvhp, OCI_HTYPE_SERVER,
(size_t) 0, (dvoid **) 0);
(void) OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &svchp, OCI_HTYPE_SVCCTX,
(size_t) 0, (dvoid **) 0);
(void) OCIServerAttach( srvhp, errhp, (text )“TEST”, strlen(“TEST”), 0);
/
set attribute server context in the service context */
(void) OCIAttrSet( (dvoid *) svchp, OCI_HTYPE_SVCCTX, (dvoid *)srvhp,
(ub4) 0, OCI_ATTR_SERVER, (OCIError *) errhp);
(void) OCIHandleAlloc((dvoid *) envhp, (dvoid **)&authp,
(ub4) OCI_HTYPE_SESSION, (size_t) 0, (dvoid **) 0);
(void) OCIAttrSet((dvoid *) authp, (ub4) OCI_HTYPE_SESSION,
(dvoid *) “test”, (ub4) strlen(“test”),
(ub4) OCI_ATTR_USERNAME, errhp);
(void) OCIAttrSet((dvoid *) authp, (ub4) OCI_HTYPE_SESSION,
(dvoid *) “oa-123”, (ub4) strlen((char *)“oa-123”),
(ub4) OCI_ATTR_PASSWORD, errhp);
r=OCISessionBegin ( svchp, errhp, authp, OCI_CRED_RDBMS,
(ub4) OCI_DEFAULT);
if(r!=0)
printf(“OCISessionBegin error %d\n”,r);
(void) OCIAttrSet((dvoid *) svchp, (ub4) OCI_HTYPE_SVCCTX,
(dvoid *) authp, (ub4) 0,
(ub4) OCI_ATTR_SESSION, errhp);
r=OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &stmthp,
OCI_HTYPE_STMT, (size_t) 0, (dvoid **) 0);
if(r!=0)
printf(“OCIHandleAlloc stmthp error %d\n”,r);

//删除表
OCIStmtPrepare(stmthp, errhp,
“drop table event”,strlen(“drop table event”),OCI_NTV_SYNTAX, OCI_DEFAULT);
OCIStmtExecute(svchp, stmthp, errhp, (ub4) 1, (ub4) 0,
(CONST OCISnapshot *) 0, (OCISnapshot *) 0,
(ub4) OCI_DEFAULT);

OCITransCommit(svchp, errhp, (ub4)0);
if (stmthp) {
OCIHandleFree((dvoid *)stmthp, (ub4) OCI_HTYPE_STMT);
}
OCISessionEnd((OCISvcCtx *)svchp, (OCIError *)errhp,
(OCISession *)authp, (ub4) OCI_DEFAULT);
OCIServerDetach((OCIServer *)srvhp, (OCIError )errhp,
(ub4)OCI_DEFAULT);
/
Free the handles */
if (authp) {
OCIHandleFree((dvoid *)authp, (ub4) OCI_HTYPE_SESSION);
}
if (svchp) {
OCIHandleFree((dvoid *)svchp, (ub4) OCI_HTYPE_SVCCTX);
}
if (srvhp) {
OCIHandleFree((dvoid *)srvhp, (ub4) OCI_HTYPE_SERVER);
}
if (errhp) {
OCIHandleFree((dvoid *)errhp, (ub4) OCI_HTYPE_ERROR);
}
if (envhp) {
OCIHandleFree((dvoid *)envhp, (ub4) OCI_HTYPE_ENV);
}
}

编译:
set path=c:\mingw\bin
gcc IcvOracle.c -I C:\oracle\product\10.2.0\db_1\OCI\include -L C:\oracle\product\10.2.0\db_1\OCI\lib\MSVC -loci

运行:
set path=C:\oracle\product\10.2.0\db_1\BIN;C:\WINDOWS\system32\wbem
a.exe

标签:oci,OCI,ub4,void,errhp,dll,HTYPE,oracle,dvoid
来源: https://blog.csdn.net/lz280696516/article/details/119318647