1 // OracleCallInterface.cpp : Defines the entry point for the console application.
2 //
3
4 #include "stdafx.h"
5 #include "oci.h"
6 #include<stdio.h>
7 #include<stdlib.h>
8 #include<string.h>
9
10 typedef struct
11 {
12 char empno[10];
13 char ename[10];
14 int age;
15 char sex[4];
16 }cdr;
17
18 int _tmain(int argc, _TCHAR* argv[])
19 {
20 OCIEnv *m_envhp;
21 OCIError *m_errhp;
22 OCIServer *m_srvhp;
23 OCISvcCtx *m_svchp;
24 OCIStmt *m_stmthp;
25 sword swResult;
26 cdr t_cdr;
27 OCIDefine *hDefine = (OCIDefine *) 0;
28 OCIDefine *hDefine1 = (OCIDefine *) 0;
29 OCIDefine *hDefine2 = (OCIDefine *) 0;
30 OCIDefine *hDefine3 = (OCIDefine *) 0;
31 OCIDefine *hDefine4 = (OCIDefine *) 0;
32 int i;
33
34 char username[255];
35 char password[30];
36 char dbname[30];
37 char szSqlStr[255];
38
39 strcpy(username,"liugang");
40 strcpy(password,"LiuGang");
41 strcpy(dbname,"LinkAge");
42
43 OCIInitialize((ub4)OCI_DEFAULT,(dvoid *)0,(dvoid *(*)(dvoid *,size_t))0,(dvoid *(*)(dvoid *,dvoid *,size_t))0,(void (*)(dvoid *,dvoid *)) 0 );
44 OCIEnvInit((OCIEnv **)&m_envhp, OCI_DEFAULT, (size_t) 0,(dvoid **) 0 );
45
46 OCIHandleAlloc( (dvoid *)m_envhp, (dvoid **)&m_errhp,OCI_HTYPE_ERROR,(size_t) 0, (dvoid **) 0);
47 OCIHandleAlloc( (dvoid *)m_envhp, (dvoid **)&m_srvhp,OCI_HTYPE_SERVER,(size_t) 0, (dvoid **) 0);
48 OCIHandleAlloc( (dvoid *)m_envhp, (dvoid **)&m_svchp,OCI_HTYPE_SVCCTX,(size_t) 0, (dvoid **) 0);
49 OCIHandleAlloc( (dvoid *)m_envhp, (dvoid **)&m_stmthp,OCI_HTYPE_STMT,(size_t) 0, (dvoid **)0);
50
51 OCIServerAttach(m_srvhp, m_errhp,(text *)dbname, strlen(dbname),(ub4) OCI_DEFAULT);
52 OCILogon(m_envhp,m_errhp,&m_svchp,(text *)username,strlen(username),(text *)password,strlen(password),(text *)dbname,strlen(dbname));
53
54
55 sprintf( szSqlStr,"%s", "SELECT EMPNO,ENAME,AGE,SEX FROM EMP ORDER BY EMPNO ASC");
56 OCIStmtPrepare(m_stmthp, m_errhp, (text*)szSqlStr, (ub4)strlen(szSqlStr),(ub4) OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT);
57
58 OCIDefineByPos(m_stmthp,&hDefine1,m_errhp,1,&t_cdr.empno,sizeof(t_cdr.empno), SQLT_CHR, NULL, NULL, NULL, OCI_DEFAULT);
59 OCIDefineByPos(m_stmthp,&hDefine2,m_errhp,2,&t_cdr.ename,sizeof(t_cdr.ename), SQLT_CHR, NULL, NULL, NULL, OCI_DEFAULT);
60 OCIDefineByPos(m_stmthp,&hDefine3,m_errhp,3,&t_cdr.age,sizeof(t_cdr.age), SQLT_INT, NULL, NULL, NULL, OCI_DEFAULT);
61 OCIDefineByPos(m_stmthp,&hDefine4,m_errhp,4,&t_cdr.sex,sizeof(t_cdr.sex), SQLT_CHR, NULL, NULL, NULL, OCI_DEFAULT);
62 OCIDefineArrayOfStruct(hDefine, m_errhp, sizeof(t_cdr), 0, 0, 0);
63 OCIStmtExecute( m_svchp, m_stmthp, m_errhp, (ub4) 1, (ub4) 0,(OCISnapshot *) NULL,(OCISnapshot *) NULL, (ub4)OCI_STMT_SCROLLABLE_READONLY );
64
65 int rows_fetched;
66
67 do
68 {
69 for(i=0;i<10;i++)
70 {
71 if(t_cdr.empno[i]==' ')
72 {
73 t_cdr.empno[i]='\0';
74 break;
75 }
76 }
77 for(i=0;i<10;i++)
78 {
79 if(t_cdr.ename[i]==' ')
80 {
81 t_cdr.ename[i]='\0';
82 break;
83 }
84 }
85 for(i=0;i<4;i++)
86 {
87 if(t_cdr.sex[i]==' ')
88 {
89 t_cdr.sex[i]='\0';
90 break;
91 }
92 }
93
94 printf("%s\t%s\t%d\t%s\n",t_cdr.empno,t_cdr.ename,t_cdr.age,t_cdr.sex);
95 }
96 while ((swResult=OCIStmtFetch2(m_stmthp,m_errhp,1,OCI_FETCH_NEXT,1,OCI_DEFAULT))!=OCI_NO_DATA);
97 OCIAttrGet((CONST void *)m_stmthp,OCI_HTYPE_STMT,(void *)&rows_fetched,(ub4 *)sizeof(rows_fetched),OCI_ATTR_ROW_COUNT ,m_errhp);
98 printf("\n总共记录数:%d",rows_fetched);
99 OCILogoff( m_svchp, m_errhp );
100 OCIServerDetach( m_srvhp, m_errhp, OCI_DEFAULT );
101 OCIHandleFree((dvoid *) m_stmthp, OCI_HTYPE_STMT);
102 OCIHandleFree((dvoid *) m_svchp, OCI_HTYPE_SVCCTX);
103 OCIHandleFree((dvoid *) m_srvhp, OCI_HTYPE_SERVER);
104 OCIHandleFree((dvoid *) m_errhp, OCI_HTYPE_ERROR);
105 getchar();
106 return 0;
107 }
108
109
在这里,OCIInitialize()和OCIEnvInit()函数可以被OCIEnvCreate替换:
OCIEnvCreate((OCIEnv **)&m_envhp,OCI_DEFAULT,(dvoid *)0,(dvoid *(*)(dvoid *,size_t))0,(dvoid *(*)(dvoid *,dvoid *,size_t))0,(void (*)(dvoid *,dvoid *))0,(size_t)0,(dvoid **)0);
从数据库中的emp表中读出的数据,
结果如下:
偶然发现,还有一种类型是SQLT_STR,如果把SQLT_CHR替换成SQLT_STR就可以不用手动在末尾加上'\0',终于解决了
posted on 2007-05-24 20:42
LG 阅读(4820)
评论(0) 编辑 收藏 引用 所属分类:
CPlusPlus