Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1063392
  • 博文数量: 284
  • 博客积分: 8223
  • 博客等级: 中将
  • 技术积分: 3188
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-01 13:26
文章分类

全部博文(284)

文章存档

2012年(18)

2011年(33)

2010年(83)

2009年(147)

2008年(3)

分类: Oracle

2009-05-30 10:05:54

   OCI程序,折腾了我好几天,看着程序终于运行成功,很开心,这里分享下自己的劳动成果。
   1. 首先共享下自己实验的程序。
#include
#include
#include
#include
#include
int main(int argc, char* argv[])
{
  OCIEnv* envhp=0;
  OCIError* errhp=0;
  OCIServer* srvhp=0;
  OCISvcCtx* svchp=0;
  OCIStmt* stmthp=0;
 
  text username[30]="ktest";
  text password[30]="ktest";
  text dbname[30]="ORCL";
  text msg[100]="insert into test values(1)";
  int i;
 
  i = OCIEnvCreate( &envhp, OCI_DEFAULT,0 , 0 , 0, 0, 0,0);
  i = OCIHandleAlloc((dvoid *)envhp, (dvoid**)&errhp, OCI_HTYPE_ERROR, 0, 0);
  i = OCIHandleAlloc((dvoid *)envhp, (dvoid**)&srvhp, OCI_HTYPE_SERVER,0,0);
  i = OCIServerAttach(srvhp, errhp, (text *) 0, 0 ,(ub4) OCI_DEFAULT);
  i = OCIHandleAlloc(envhp, (dvoid**)&svchp, OCI_HTYPE_SVCCTX,0,0);
  i = OCIHandleAlloc(envhp, (dvoid**)&stmthp, OCI_HTYPE_STMT,0,0);
 
  i = OCIAttrSet(svchp, OCI_HTYPE_SVCCTX, srvhp, 0 , OCI_ATTR_SERVER, errhp);
  i = OCILogon(envhp, errhp, &svchp, (text *)"ktest", 5, (text *)"ktest", 5,(text *)"ORCL", 4);

 i =  OCIStmtPrepare(stmthp, errhp, (text *)msg , strlen(msg), OCI_NTV_SYNTAX,0);
 i =  OCIStmtExecute( svchp, stmthp, errhp ,1, 0, 0, 0,0);
 i =  OCITransCommit(svchp, errhp, 0);
}
2. 程序编译参数如下
cc -g -o  test ser.c -I/export/home/oracle/oracle/product/9.2/plsql/public -I/export/home/oracle/oracle/product/9.2/rdbms/demo -I/export/home/oracle/oracle/product/9.2/network/public  -I/export/home/oracle/oracle/product/9.2/rdbms/public -L/export/home/oracle/oracle/product/9.2/rdbms/lib32 -L/export/home/oracle/oracle/product/9.2/lib32 -lclntsh
3. 很奇怪,使用上面的程序编译OK,但初次执行时,程序OCIEnvCreate总是返回失败(-1)。折腾了几个星期也没结果,网上有很多意见,说文件夹属性啊,用户权限什么的,但是自己尝试始终不行,后来,把公司历史数据库的程序在这台主机上编译运行后,自己这个程序也就可以正常运行了,肯定有什么参数被修改了。但仔细查看了公司程序的编译文件,没有发现什么特别的地方,这个问题,我会继续研究下去,争取早日找到原因。
阅读(1089) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~