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

全部博文(284)

文章存档

2012年(18)

2011年(33)

2010年(83)

2009年(147)

2008年(3)

分类: C/C++

2009-11-26 16:25:51

#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;

}



实现从库中读取数据的操作;

编译:

cc -xarch=generic64 -g -o test ser.c -I/export/home/oracle/plsql/public -I/export/home/oracle/rdbms/demo -I/export/home/oracle/network/public -I/export/home/oracle/rdbms/public -L/export/home/oracle/rdbms/lib -L/export/home/oracle/lib -lclntsh

   在没有增加-xarch=generic64 参数时,编译会报错说

ld: 致命的: 文件 /export/home/oracle/lib/libclntsh.so:错误的 ELF 类型:ELFCLASS64

   后来检查发现oracle分32位库和64位库两种编译库,由于我的代码使用lib就应该选择了64位库来进行编译,虽然系统本身是32位的,但没有使用指定64位的编译参数,所以出现问题。有两种解决方法:

   1. 将编译中使用的lib修改为lib32。使用32位方式进行编译,生成32位程序,由于在64位机上可以运行32位程序,则一起OK;

   2. 增加-xarch=generic64 参数,使用64位lib生成64 位代码,从而代码可以正常执行。

  在程序出现问题的时候可以使用以下语句进行检查:
 

text errbuf[512];
int errcode;

OCIErrorGet ((dvoid *) errhp,  1, NULL, &errcode, errbuf, sizeof(errbuf), OCI_HTYPE_ERROR);


阅读(3723) | 评论(0) | 转发(0) |
0

上一篇:硬盘和内存的故事

下一篇:宫心计

给主人留下些什么吧!~~