OCI中使用替代变量访问ORACLE数据库
1
2 #include " oci.h "
3 #include < stdio.h >
4 #include < stdlib.h >
5 #include < string .h >
6
7
8 void myfflush()
9 {
10 eb1 buf[ 50 ];
11 fgets(( char * ) buf, 50 , stdin);
12 }
13
14 int main()
15 {
16 OCIEnv * m_envhp;
17 OCIError * m_errhp;
18 OCIServer * m_srvhp;
19 OCISvcCtx * m_svchp;
20 OCIStmt * m_stmthp;
21 OCIBind * bnd1p = NULL;
22 OCIBind * bnd2p = NULL;
23
24 char username[ 255 ];
25 char password[ 30 ];
26 char dbname[ 30 ];
27 char szSqlStr[ 255 ];
28 char empname[ 100 ];
29 char empno[ 10 ];
30
31 strcpy(username, " liugang " );
32 strcpy(password, " LiuGang " );
33 strcpy(dbname, " LinkAge " );
34
35 int i;
36 for (i = 0 ;i < 100 ;i ++ )
37 empname[i] = ' \0 ' ;
38
39 OCIInitialize((ub4)OCI_DEFAULT,(dvoid * ) 0 ,(dvoid * ( * )(dvoid * ,size_t)) 0 ,(dvoid * ( * )(dvoid * ,dvoid * ,size_t)) 0 ,( void ( * )(dvoid * ,dvoid * )) 0 );
40 OCIEnvInit((OCIEnv ** ) & m_envhp, OCI_DEFAULT, (size_t) 0 ,(dvoid ** ) 0 );
41
42 OCIHandleAlloc( (dvoid * )m_envhp, (dvoid ** ) & m_errhp,OCI_HTYPE_ERROR,(size_t) 0 , (dvoid ** ) 0 );
43 OCIHandleAlloc( (dvoid * )m_envhp, (dvoid ** ) & m_srvhp,OCI_HTYPE_SERVER,(size_t) 0 , (dvoid ** ) 0 );
44 OCIHandleAlloc( (dvoid * )m_envhp, (dvoid ** ) & m_svchp,OCI_HTYPE_SVCCTX,(size_t) 0 , (dvoid ** ) 0 );
45 OCIHandleAlloc( (dvoid * )m_envhp, (dvoid ** ) & m_stmthp,OCI_HTYPE_STMT,(size_t) 0 , (dvoid ** ) 0 );
46
47 OCIServerAttach(m_srvhp, m_errhp,(text * )dbname, strlen(dbname),(ub4) OCI_DEFAULT);
48 OCILogon(m_envhp,m_errhp, & m_svchp,(text * )username,strlen(username),(text * )password,strlen(password),(text * )dbname,strlen(dbname));
49
50 OCIDefine * defnp0 = (OCIDefine * ) 0 ;
51
52 printf( " Please input your emp number: " );
53 scanf( " %s " ,empno);
54
55 myfflush();
56
57
58 sprintf( szSqlStr, " %s " , " SELECT ENAME FROM EMP WHERE EMPNO = :empno " );
59
60 OCIStmtPrepare(m_stmthp, m_errhp, (text * )szSqlStr, (ub4)strlen(szSqlStr),(ub4) OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT);
61 int m = sizeof (empno);
62
63 // OCIBindByName(m_stmthp, &bnd1p, m_errhp, (text *)":empno",-1, (ub1 *) empno,(sword) sizeof(empno)-1, SQLT_CHR, (dvoid *) 0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT);
64
65 OCIBindByPos(m_stmthp, & bnd1p, m_errhp, 1 , (dvoid * )empno, sizeof (empno) - 1 , SQLT_CHR, 0 , 0 , 0 , 0 , 0 , (ub4) OCI_DEFAULT);
66
67 OCIDefineByPos( m_stmthp, & defnp0, m_errhp, 1 , (dvoid * )empname, 100 , SQLT_STR,(dvoid * ) 0 , (ub2 * ) 0 , (ub2 * ) 0 , OCI_DEFAULT);
68
69 OCIStmtExecute( m_svchp, m_stmthp, m_errhp, (ub4) 1 , (ub4) 0 ,(OCISnapshot * ) NULL,(OCISnapshot * ) NULL, (ub4) OCI_DEFAULT);
70
71 OCILogoff( m_svchp, m_errhp );
72 if (empname[ 0 ] == ' \0 ' )
73 strcpy(empname, " 无此人 " );
74 printf( " The employee name is:%s " ,empname);
75
76 OCIServerDetach( m_srvhp, m_errhp, OCI_DEFAULT );
77 OCIHandleFree((dvoid * ) m_stmthp, OCI_HTYPE_STMT);
78 OCIHandleFree((dvoid * ) m_svchp, OCI_HTYPE_SVCCTX);
79 OCIHandleFree((dvoid * ) m_srvhp, OCI_HTYPE_SERVER);
80 OCIHandleFree((dvoid * ) m_errhp, OCI_HTYPE_ERROR);
81 getchar();
82 return 1 ;
83 }
84
在对:empno变量进行绑定的时候,和以往差不多,有按名称绑定和位置绑定两种方法,在上面代码中使用的是按位置进行的绑定,按名称进行绑定被注释了,有兴趣的可以自己研究一下,文档中推荐使用的是按位置绑定,具体的原因还没有查清楚,希望知情者提供线索......
2 #include " oci.h "
3 #include < stdio.h >
4 #include < stdlib.h >
5 #include < string .h >
6
7
8 void myfflush()
9 {
10 eb1 buf[ 50 ];
11 fgets(( char * ) buf, 50 , stdin);
12 }
13
14 int main()
15 {
16 OCIEnv * m_envhp;
17 OCIError * m_errhp;
18 OCIServer * m_srvhp;
19 OCISvcCtx * m_svchp;
20 OCIStmt * m_stmthp;
21 OCIBind * bnd1p = NULL;
22 OCIBind * bnd2p = NULL;
23
24 char username[ 255 ];
25 char password[ 30 ];
26 char dbname[ 30 ];
27 char szSqlStr[ 255 ];
28 char empname[ 100 ];
29 char empno[ 10 ];
30
31 strcpy(username, " liugang " );
32 strcpy(password, " LiuGang " );
33 strcpy(dbname, " LinkAge " );
34
35 int i;
36 for (i = 0 ;i < 100 ;i ++ )
37 empname[i] = ' \0 ' ;
38
39 OCIInitialize((ub4)OCI_DEFAULT,(dvoid * ) 0 ,(dvoid * ( * )(dvoid * ,size_t)) 0 ,(dvoid * ( * )(dvoid * ,dvoid * ,size_t)) 0 ,( void ( * )(dvoid * ,dvoid * )) 0 );
40 OCIEnvInit((OCIEnv ** ) & m_envhp, OCI_DEFAULT, (size_t) 0 ,(dvoid ** ) 0 );
41
42 OCIHandleAlloc( (dvoid * )m_envhp, (dvoid ** ) & m_errhp,OCI_HTYPE_ERROR,(size_t) 0 , (dvoid ** ) 0 );
43 OCIHandleAlloc( (dvoid * )m_envhp, (dvoid ** ) & m_srvhp,OCI_HTYPE_SERVER,(size_t) 0 , (dvoid ** ) 0 );
44 OCIHandleAlloc( (dvoid * )m_envhp, (dvoid ** ) & m_svchp,OCI_HTYPE_SVCCTX,(size_t) 0 , (dvoid ** ) 0 );
45 OCIHandleAlloc( (dvoid * )m_envhp, (dvoid ** ) & m_stmthp,OCI_HTYPE_STMT,(size_t) 0 , (dvoid ** ) 0 );
46
47 OCIServerAttach(m_srvhp, m_errhp,(text * )dbname, strlen(dbname),(ub4) OCI_DEFAULT);
48 OCILogon(m_envhp,m_errhp, & m_svchp,(text * )username,strlen(username),(text * )password,strlen(password),(text * )dbname,strlen(dbname));
49
50 OCIDefine * defnp0 = (OCIDefine * ) 0 ;
51
52 printf( " Please input your emp number: " );
53 scanf( " %s " ,empno);
54
55 myfflush();
56
57
58 sprintf( szSqlStr, " %s " , " SELECT ENAME FROM EMP WHERE EMPNO = :empno " );
59
60 OCIStmtPrepare(m_stmthp, m_errhp, (text * )szSqlStr, (ub4)strlen(szSqlStr),(ub4) OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT);
61 int m = sizeof (empno);
62
63 // OCIBindByName(m_stmthp, &bnd1p, m_errhp, (text *)":empno",-1, (ub1 *) empno,(sword) sizeof(empno)-1, SQLT_CHR, (dvoid *) 0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT);
64
65 OCIBindByPos(m_stmthp, & bnd1p, m_errhp, 1 , (dvoid * )empno, sizeof (empno) - 1 , SQLT_CHR, 0 , 0 , 0 , 0 , 0 , (ub4) OCI_DEFAULT);
66
67 OCIDefineByPos( m_stmthp, & defnp0, m_errhp, 1 , (dvoid * )empname, 100 , SQLT_STR,(dvoid * ) 0 , (ub2 * ) 0 , (ub2 * ) 0 , OCI_DEFAULT);
68
69 OCIStmtExecute( m_svchp, m_stmthp, m_errhp, (ub4) 1 , (ub4) 0 ,(OCISnapshot * ) NULL,(OCISnapshot * ) NULL, (ub4) OCI_DEFAULT);
70
71 OCILogoff( m_svchp, m_errhp );
72 if (empname[ 0 ] == ' \0 ' )
73 strcpy(empname, " 无此人 " );
74 printf( " The employee name is:%s " ,empname);
75
76 OCIServerDetach( m_srvhp, m_errhp, OCI_DEFAULT );
77 OCIHandleFree((dvoid * ) m_stmthp, OCI_HTYPE_STMT);
78 OCIHandleFree((dvoid * ) m_svchp, OCI_HTYPE_SVCCTX);
79 OCIHandleFree((dvoid * ) m_srvhp, OCI_HTYPE_SERVER);
80 OCIHandleFree((dvoid * ) m_errhp, OCI_HTYPE_ERROR);
81 getchar();
82 return 1 ;
83 }
84
运行结果如下:
在这里还存在这样一个问题:SELECT ENAME FROM EMP WHERE EMPNO = :empno不可以写成SELECT ENAME FROM EMP WHERE EMPNO = ':empno',虽然在数据库中EMPNO是VARCHAR2类型的,我个人认为在使用OCIBindByPos的时候,已经指定了绑定的变量是SQLT_CHR类型的,所以就没有必要在语句中使用':empno'了,这只是一家之言,希望有不同想法的同志可以提出,共同讨论......