数据库: Release 11.1.0.6.0;
OS: Windows XP;
编译环境: gcc version 4.5.1
code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "sqlda.h"
#include "sqlcpr.h"
EXEC SQL INCLUDE sqlca;
/*RELEASE_CURSOR=YES 使PROC 在执行完后释放与嵌入SQL有关资源*/
EXEC ORACLE OPTION (RELEASE_CURSOR = YES);
EXEC SQL BEGIN DECLARE SECTION;
varchar vc_user[20];
long al_empno=0;
char ac_ename[11]="";
char ac_hiredate[20]="";
double af_sal=0;
EXEC SQL VAR ac_ename IS STRING(11);
EXEC SQL VAR ac_hiredate IS STRING(20);
EXEC SQL END DECLARE SECTION;
/*错误处理函数*/
void sql_error(char *msg)
{
printf("/n%s,%ld,%s/n", msg,sqlca.sqlcode,(char *)sqlca.sqlerrm.sqlerrmc);
EXEC SQL ROLLBACK RELEASE;
exit(-1);
}
main()
{
EXEC SQL WHENEVER SQLERROR DO sql_error("ORACLE ERROR: ");
/*连接数据库*/
strcpy(vc_user.arr,"scott/tiger@orcl");
vc_user.len=16;
exec sql connect :vc_user;
EXEC SQL DECLARE cur_emp CURSOR FOR
SELECT EMPNO, ENAME,to_char(HIREDATE,'yyyy/mm/dd hh24:mi:ss'),SAL FROM EMP;
EXEC SQL OPEN cur_emp;
while(1)
{
al_empno=0;
strcpy(ac_ename,"");
strcpy(ac_hiredate,"");
af_sal=0;
EXEC SQL FETCH cur_emp INTO :al_empno,:ac_ename,:ac_hiredate,:af_sal;
if( sqlca.sqlcode == 1403)
{
break;
}
printf("empno=%ld,ename=%s,hiredate=%s,sal=%lf/n",al_empno,ac_ename,ac_hiredate,af_sal);
}
EXEC SQL CLOSE cur_emp;
EXEC SQL ROLLBACK WORK RELEASE;
}
编译文件:gcc -o conn conn.c -I %ORACLE%/precomp/public -L %ORACLE%/precomp/LIB -l orasql11
生成名为conn.exe文件,下面是运行conn.exe的结果。
*********************************************************************************
D:/codeblock/db>conn
empno=7369,ename=SMITH,hiredate=1980/12/17 00:00:00,sal=800.000000
empno=7499,ename=ALLEN,hiredate=1981/02/20 00:00:00,sal=1600.000000
empno=7521,ename=WARD,hiredate=1981/02/22 00:00:00,sal=1250.000000
empno=7566,ename=JONES,hiredate=1981/04/02 00:00:00,sal=2975.000000
empno=7654,ename=MARTIN,hiredate=1981/09/28 00:00:00,sal=1250.000000
empno=7698,ename=BLAKE,hiredate=1981/05/01 00:00:00,sal=2850.000000
empno=7782,ename=CLARK,hiredate=1981/06/09 00:00:00,sal=2450.000000
empno=7788,ename=SCOTT,hiredate=1987/04/19 00:00:00,sal=3000.000000
empno=7839,ename=KING,hiredate=1981/11/17 00:00:00,sal=5000.000000
empno=7844,ename=TURNER,hiredate=1981/09/08 00:00:00,sal=1500.000000
empno=7876,ename=ADAMS,hiredate=1987/05/23 00:00:00,sal=1100.000000
empno=7900,ename=JAMES,hiredate=1981/12/03 00:00:00,sal=950.000000
empno=7902,ename=FORD,hiredate=1981/12/03 00:00:00,sal=3000.000000
empno=7934,ename=MILLER,hiredate=1982/01/23 00:00:00,sal=1300.000000
********************************************************************************