当前位置: 技术问答>linux和unix
linux下利用oci开发oracle的问题?
来源: 互联网 发布时间:2015-05-31
本文导语: 回答下面问题中的一个或多个均有分,不够可以再加! 1.oci在什么地方可以下载到? 2.在linux下如何配置oci?(最好有相关文档) 3.linux下用oci开发oracle的相关代码或例子程序。 多谢! | 安装or...
回答下面问题中的一个或多个均有分,不够可以再加!
1.oci在什么地方可以下载到?
2.在linux下如何配置oci?(最好有相关文档)
3.linux下用oci开发oracle的相关代码或例子程序。
多谢!
1.oci在什么地方可以下载到?
2.在linux下如何配置oci?(最好有相关文档)
3.linux下用oci开发oracle的相关代码或例子程序。
多谢!
|
安装oracle后,在$ORACLE_HOME/rdbms/demo目录下有oci.h。这个头文件要包含进来。
编译时要加上这些路径:-L$(ORACLE_HOME)/lib -L$(ORACLE_HOME)/rdbms/lib
好像这些库也是必需的,记不清了:-lclntsh -lrt -lpthread
例子可以在oracle文档中找到,很详细。
编译时要加上这些路径:-L$(ORACLE_HOME)/lib -L$(ORACLE_HOME)/rdbms/lib
好像这些库也是必需的,记不清了:-lclntsh -lrt -lpthread
例子可以在oracle文档中找到,很详细。
|
我给你一个例子,但不是一两句能够说清楚,不要怪我灌水啊◎!
#include
#include
#include
#include // oci head file
static text *username = (text *) "internal";
static text *password = (text *) "oracle";
static text *insert=(text *) "INSERT INTO dept(deptno, dname, loc)
VALUES (:1, :2, :LOC)";
static void error_proc(dvoid *errhp,sword status);
static void flush();
void main()
{
sword status,deptno;
text *dname, *loc, *cp;
text buf[3];
sb4 dnamelen=14;
sb4 loclen=13;
sb2 dname_ind, loc_ind;
OCIEnv *envhp;
OCIError *errhp;
OCIServer *srvhp;
OCISvcCtx *svchp;
OCISession *authp;
OCIStmt *stmthp;
OCIBind *bnd1p = (OCIBind *) 0;
OCIBind *bnd2p = (OCIBind *) 0;
OCIBind *bnd3p = (OCIBind *) 0;
//create OCI enviroment
OCIEnvCreate(&envhp,OCI_THREADED,(dvoid *)0,(void *(*)()) 0,
(void *(*)()) 0, (void (*)()) 0, 0,(dvoid **) 0);
OCIHandleAlloc((dvoid *) envhp,(dvoid **) &errhp, OCI_HTYPE_ERROR,
(size_t) 0, (dvoid **) 0);
OCIHandleAlloc((dvoid *) envhp,(dvoid **) &srvhp, OCI_HTYPE_SERVER,
(size_t) 0, (dvoid **) 0);
OCIHandleAlloc((dvoid *) envhp,(dvoid **) &svchp, OCI_HTYPE_SVCCTX,
(size_t) 0, (dvoid **) 0);
// connect to database oradb"
if (OCIServerAttach(srvhp, errhp, (text *)"oradb", strlen("oradb"),
0) == OCI_SUCCESS)
printf("n it has been success to connect to oradb!n");
else
printf("Connection errorn");
// set attribute
OCIAttrSet((dvoid *) svchp, OCI_HTYPE_SVCCTX,(dvoid *)srvhp,(ub4)0,
OCI_ATTR_SERVER,(OCIError *) errhp);
OCIHandleAlloc((dvoid *)envhp,(dvoid **)&authp,OCI_HTYPE_SESSION,
(size_t) 0, (dvoid **) 0); //assessment hp
// set account used by hp
OCIAttrSet((dvoid *) authp,OCI_HTYPE_SESSION,username,(ub4)strlen((char *)username),OCI_ATTR_USERNAME,errhp);
OCIAttrSet((dvoid *) authp,OCI_HTYPE_SESSION,password,(ub4)strlen((char *)password),OCI_ATTR_PASSWORD,errhp);
error_proc(errhp, OCISessionBegin(svchp,errhp,authp,OCI_CRED_RDBMS,(ub4) OCI_DEFAULT));
OCIAttrSet((dvoid *)svchp,OCI_HTYPE_SVCCTX,authp,(ub4) 0,OCI_ATTR_SESSION,errhp);
OCIHandleAlloc((dvoid *)envhp,(dvoid **)&stmthp,OCI_HTYPE_STMT,(size_t) 0,(dvoid **) 0);
error_proc(errhp,OCIHandleAlloc((dvoid *)envhp, (dvoid **)&stmthp,
OCI_HTYPE_STMT, (size_t)dnamelen + 2 + loclen + 2,
(dvoid **) &dname));
loc = (text *)(dname + dnamelen + 2);
//prepare and execute SQL statement
error_proc(errhp, OCIStmtPrepare(stmthp,errhp,insert,(ub4) strlen((char *) insert),OCI_NTV_SYNTAX,OCI_DEFAULT));
status = OCIBindByPos(stmthp,&bnd1p,errhp,1, (dvoid *) &deptno, (sword) sizeof(deptno),SQLT_INT,(dvoid *) 0,(ub2 *) 0,(ub2 *) 0,(ub4) 0,(ub4 *) 0,OCI_DEFAULT);
status |= OCIBindByPos(stmthp,&bnd2p,errhp,2,(dvoid *) &dname, dnamelen + 1,SQLT_STR, &dname_ind, (ub2 *) 0,(ub2 *) 0,(ub4) 0,(ub4 *) 0,OCI_DEFAULT);
status |= OCIBindByName(stmthp,&bnd3p,errhp,(text *) ":LOC",-1, (dvoid *) loc, loclen+1,SQLT_STR, &loc_ind, (ub2 *) 0,(ub2 *) 0,(ub4) 0,(ub4 *) 0,OCI_DEFAULT);
if (status){
error_proc(errhp,status);
printf("Bind errorn");
OCIHandleFree((dvoid *) envhp, OCI_HTYPE_ENV);
exit(-1);
}
for (; ;){
printf("Number(2 bits,ENTER for exit): ");
fgets(buf, 3, stdin);
cp = (text *) strchr((char *) buf, 'n');
if(cp==buf){
OCIHandleFree((dvoid *) envhp, OCI_HTYPE_ENV);
exit(0);
}
else{
if (cp) *cp = '';
else flush();
deptno = atoi(buf);
}
printf("n Name : ");
fgets(dname,dnamelen =1,stdin);
cp = (text *) strchr((char *) dname, 'n');
if(cp==dname){
dname_ind = -1;
}
else{
if (cp) *cp = '';
else flush();
dname_ind=0;
}
printf("n Place: ");
fgets(loc, (int) loclen + 1, stdin);
cp = (text *) strchr((char *) loc, 'n');
if(cp==loc){
loc_ind=-1;
}
else{
loc_ind=0;
if (cp) *cp = '';
else flush();
}
if((status = OCIStmtExecute(svchp, stmthp, errhp, (ub4) 1,
(ub4) 0, (CONST OCISnapshot *) NULL,(OCISnapshot *) NULL,
OCI_DEFAULT)) && status != OCI_SUCCESS_WITH_INFO){
error_proc(errhp,status);
printf("Execute errorn");
OCIHandleFree((dvoid *) envhp, OCI_HTYPE_ENV);
exit(-1);
}
if(status = OCITransCommit(svchp,errhp,0))
{
error_proc(errhp,status);
OCIHandleFree((dvoid *) envhp,OCI_HTYPE_ENV);
exit(-1);
}
else
printf("Insertation Success!n");
}
// over the dialog
if (OCISessionEnd(svchp,errhp,authp,OCI_DEFAULT) == OCI_SUCCESS)
printf("n %s dialog is over !",username);
// disrupt the connect
if (OCIServerDetach(srvhp,errhp,OCI_DEFAULT) == OCI_SUCCESS)
printf("noradb database connect is disrupted!");
//free all kinds of hp
OCIHandleFree((dvoid *) envhp, OCI_HTYPE_ENV);
}
void flush() {
char buf[128];
fgets(buf, 128, stdin);
}
//deal with error
static void error_proc(dvoid *errhp, sword status)
{
text errbuf[512];
sb4 errcode;
switch (status)
{
case OCI_SUCCESS:
break;
case OCI_SUCCESS_WITH_INFO:
(void) printf("OCI ERROR CODE:OCI_SUCCESS_WITH_INFOn");
break;
case OCI_NEED_DATA:
(void) printf("OCI ERROR CODE:OCI_NEED_DATAn");
break;
case OCI_NO_DATA:
(void) printf("OCI ERROR CODE:OCI_NO_DATAn");
break;
case OCI_ERROR:
(void) OCIErrorGet((dvoid *)errhp,(ub4) 1,NULL,&errcode,errbuf,(ub4)sizeof(errbuf),OCI_HTYPE_ERROR);
(void) printf("OCI ERROR CODE:%dnerror information: %sn",errcode,errbuf);
break;
case OCI_INVALID_HANDLE:
(void) printf("OCI ERROR CODE: OCI_INVALID_HANDLEn");
break;
case OCI_STILL_EXECUTING:
(void) printf("OCI ERROR CODE: OCI_STILL_EXECUTINGn");
break;
default:
break;
}
}
#include
#include
#include
#include // oci head file
static text *username = (text *) "internal";
static text *password = (text *) "oracle";
static text *insert=(text *) "INSERT INTO dept(deptno, dname, loc)
VALUES (:1, :2, :LOC)";
static void error_proc(dvoid *errhp,sword status);
static void flush();
void main()
{
sword status,deptno;
text *dname, *loc, *cp;
text buf[3];
sb4 dnamelen=14;
sb4 loclen=13;
sb2 dname_ind, loc_ind;
OCIEnv *envhp;
OCIError *errhp;
OCIServer *srvhp;
OCISvcCtx *svchp;
OCISession *authp;
OCIStmt *stmthp;
OCIBind *bnd1p = (OCIBind *) 0;
OCIBind *bnd2p = (OCIBind *) 0;
OCIBind *bnd3p = (OCIBind *) 0;
//create OCI enviroment
OCIEnvCreate(&envhp,OCI_THREADED,(dvoid *)0,(void *(*)()) 0,
(void *(*)()) 0, (void (*)()) 0, 0,(dvoid **) 0);
OCIHandleAlloc((dvoid *) envhp,(dvoid **) &errhp, OCI_HTYPE_ERROR,
(size_t) 0, (dvoid **) 0);
OCIHandleAlloc((dvoid *) envhp,(dvoid **) &srvhp, OCI_HTYPE_SERVER,
(size_t) 0, (dvoid **) 0);
OCIHandleAlloc((dvoid *) envhp,(dvoid **) &svchp, OCI_HTYPE_SVCCTX,
(size_t) 0, (dvoid **) 0);
// connect to database oradb"
if (OCIServerAttach(srvhp, errhp, (text *)"oradb", strlen("oradb"),
0) == OCI_SUCCESS)
printf("n it has been success to connect to oradb!n");
else
printf("Connection errorn");
// set attribute
OCIAttrSet((dvoid *) svchp, OCI_HTYPE_SVCCTX,(dvoid *)srvhp,(ub4)0,
OCI_ATTR_SERVER,(OCIError *) errhp);
OCIHandleAlloc((dvoid *)envhp,(dvoid **)&authp,OCI_HTYPE_SESSION,
(size_t) 0, (dvoid **) 0); //assessment hp
// set account used by hp
OCIAttrSet((dvoid *) authp,OCI_HTYPE_SESSION,username,(ub4)strlen((char *)username),OCI_ATTR_USERNAME,errhp);
OCIAttrSet((dvoid *) authp,OCI_HTYPE_SESSION,password,(ub4)strlen((char *)password),OCI_ATTR_PASSWORD,errhp);
error_proc(errhp, OCISessionBegin(svchp,errhp,authp,OCI_CRED_RDBMS,(ub4) OCI_DEFAULT));
OCIAttrSet((dvoid *)svchp,OCI_HTYPE_SVCCTX,authp,(ub4) 0,OCI_ATTR_SESSION,errhp);
OCIHandleAlloc((dvoid *)envhp,(dvoid **)&stmthp,OCI_HTYPE_STMT,(size_t) 0,(dvoid **) 0);
error_proc(errhp,OCIHandleAlloc((dvoid *)envhp, (dvoid **)&stmthp,
OCI_HTYPE_STMT, (size_t)dnamelen + 2 + loclen + 2,
(dvoid **) &dname));
loc = (text *)(dname + dnamelen + 2);
//prepare and execute SQL statement
error_proc(errhp, OCIStmtPrepare(stmthp,errhp,insert,(ub4) strlen((char *) insert),OCI_NTV_SYNTAX,OCI_DEFAULT));
status = OCIBindByPos(stmthp,&bnd1p,errhp,1, (dvoid *) &deptno, (sword) sizeof(deptno),SQLT_INT,(dvoid *) 0,(ub2 *) 0,(ub2 *) 0,(ub4) 0,(ub4 *) 0,OCI_DEFAULT);
status |= OCIBindByPos(stmthp,&bnd2p,errhp,2,(dvoid *) &dname, dnamelen + 1,SQLT_STR, &dname_ind, (ub2 *) 0,(ub2 *) 0,(ub4) 0,(ub4 *) 0,OCI_DEFAULT);
status |= OCIBindByName(stmthp,&bnd3p,errhp,(text *) ":LOC",-1, (dvoid *) loc, loclen+1,SQLT_STR, &loc_ind, (ub2 *) 0,(ub2 *) 0,(ub4) 0,(ub4 *) 0,OCI_DEFAULT);
if (status){
error_proc(errhp,status);
printf("Bind errorn");
OCIHandleFree((dvoid *) envhp, OCI_HTYPE_ENV);
exit(-1);
}
for (; ;){
printf("Number(2 bits,ENTER for exit): ");
fgets(buf, 3, stdin);
cp = (text *) strchr((char *) buf, 'n');
if(cp==buf){
OCIHandleFree((dvoid *) envhp, OCI_HTYPE_ENV);
exit(0);
}
else{
if (cp) *cp = '';
else flush();
deptno = atoi(buf);
}
printf("n Name : ");
fgets(dname,dnamelen =1,stdin);
cp = (text *) strchr((char *) dname, 'n');
if(cp==dname){
dname_ind = -1;
}
else{
if (cp) *cp = '';
else flush();
dname_ind=0;
}
printf("n Place: ");
fgets(loc, (int) loclen + 1, stdin);
cp = (text *) strchr((char *) loc, 'n');
if(cp==loc){
loc_ind=-1;
}
else{
loc_ind=0;
if (cp) *cp = '';
else flush();
}
if((status = OCIStmtExecute(svchp, stmthp, errhp, (ub4) 1,
(ub4) 0, (CONST OCISnapshot *) NULL,(OCISnapshot *) NULL,
OCI_DEFAULT)) && status != OCI_SUCCESS_WITH_INFO){
error_proc(errhp,status);
printf("Execute errorn");
OCIHandleFree((dvoid *) envhp, OCI_HTYPE_ENV);
exit(-1);
}
if(status = OCITransCommit(svchp,errhp,0))
{
error_proc(errhp,status);
OCIHandleFree((dvoid *) envhp,OCI_HTYPE_ENV);
exit(-1);
}
else
printf("Insertation Success!n");
}
// over the dialog
if (OCISessionEnd(svchp,errhp,authp,OCI_DEFAULT) == OCI_SUCCESS)
printf("n %s dialog is over !",username);
// disrupt the connect
if (OCIServerDetach(srvhp,errhp,OCI_DEFAULT) == OCI_SUCCESS)
printf("noradb database connect is disrupted!");
//free all kinds of hp
OCIHandleFree((dvoid *) envhp, OCI_HTYPE_ENV);
}
void flush() {
char buf[128];
fgets(buf, 128, stdin);
}
//deal with error
static void error_proc(dvoid *errhp, sword status)
{
text errbuf[512];
sb4 errcode;
switch (status)
{
case OCI_SUCCESS:
break;
case OCI_SUCCESS_WITH_INFO:
(void) printf("OCI ERROR CODE:OCI_SUCCESS_WITH_INFOn");
break;
case OCI_NEED_DATA:
(void) printf("OCI ERROR CODE:OCI_NEED_DATAn");
break;
case OCI_NO_DATA:
(void) printf("OCI ERROR CODE:OCI_NO_DATAn");
break;
case OCI_ERROR:
(void) OCIErrorGet((dvoid *)errhp,(ub4) 1,NULL,&errcode,errbuf,(ub4)sizeof(errbuf),OCI_HTYPE_ERROR);
(void) printf("OCI ERROR CODE:%dnerror information: %sn",errcode,errbuf);
break;
case OCI_INVALID_HANDLE:
(void) printf("OCI ERROR CODE: OCI_INVALID_HANDLEn");
break;
case OCI_STILL_EXECUTING:
(void) printf("OCI ERROR CODE: OCI_STILL_EXECUTINGn");
break;
default:
break;
}
}
|
3. 如果直接用oci开发,很麻烦, 我推荐你用Pro C/C++ 那要方便的多, 如果你一定要用oci开发,最好是在oci上进行封装成一定的接口, 好多驱动程序都是在oci上进程封装的。这要发了定的时间,还有技术难度。
也可以使用其他的驱动程序啊, 比如ODBC, 你可以到www.oracle.com上去下载,这就和在windows下用odbc是一样的了.
也可以使用其他的驱动程序啊, 比如ODBC, 你可以到www.oracle.com上去下载,这就和在windows下用odbc是一样的了.
|
1. 如果你装了Oracle服务端或客户端,可以在里面找到一个oci的文件夹,里面的头文件 就可以了,库已经生成。
|
2. 不要配置,只要在头文件里包含相关的头文件, 编译和链接时加上库文件就可以了。
|
here is a link:
http://download-east.oracle.com/docs/cd/A81042_01/DOC/appdev.816/a76975/toc.htm
http://download-east.oracle.com/docs/cd/A81042_01/DOC/appdev.816/a76975/toc.htm