Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2373715
  • 博文数量: 473
  • 博客积分: 12252
  • 博客等级: 上将
  • 技术积分: 4307
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-12 10:02
文章分类

全部博文(473)

文章存档

2012年(8)

2011年(63)

2010年(73)

2009年(231)

2008年(98)

分类: Oracle

2009-09-30 15:54:49

#pragma comment(lib, "C:\\ora92\\oci\\lib\\msvc\\oci.lib")
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ci.h>

static void checkerr (OCIError *p_err, sword status);


void main()
{
OCIEnv *p_env;
OCIError *p_err = (OCIError *)0;
OCISvcCtx *p_svc;
OCIStmt *p_sql;
OCIDefine *p_define1 = (OCIDefine *) 0;
OCIDefine *p_define2 = (OCIDefine *) 0;
OCIDefine *p_define3 = (OCIDefine *) 0;
OCIDate hdate;
text Ename[20];
text hdate_buf[200];
ub4 str_len;
ub1 date9[7];

text *mySql = (text *) "SELECT ename, hiredate, hiredate FROM EMP";

memset((void *) Ename, (int)'\0' , (size_t) 20);
memset((void *) hdate_buf, (int)'\0' , (size_t) 200);

printf("OCIInitialize\n");
checkerr(p_err, OCIInitialize((ub4) OCI_OBJECT,
                 (dvoid *) 0, (dvoid * (*) ()) 0,
                 (dvoid * (*) ()) 0, (void (*) ()) 0));

printf("OCIEnvInit\n");
checkerr(p_err, OCIEnvInit(&p_env, (ub4) OCI_DEFAULT,
                                (size_t) 0, (dvoid **)0));

printf("OCIHandleAlloc\n");
checkerr(p_err, OCIHandleAlloc(p_env, &p_err, OCI_HTYPE_ERROR,
                                (size_t) 0, (dvoid **) 0));

printf("OCIHandleAlloc\n");
checkerr(p_err, OCIHandleAlloc(p_env, &p_svc, OCI_HTYPE_SVCCTX,
                                (size_t) 0, (dvoid **) 0));

printf("OCIHandleAlloc\n");
checkerr(p_err, OCIHandleAlloc(p_env, &p_sql,
                                OCI_HTYPE_STMT, (size_t) 0, (dvoid **) 0));

printf("OCILogon\n");
checkerr(p_err, OCILogon(p_env, p_err, &p_svc, "SCOTT", 5, "TIGER", 5, "ra92", 5));

printf("OCIStmtPrepare\n");
printf(" %s\n",mySql);
checkerr(p_err, OCIStmtPrepare(p_sql, p_err, mySql,
                                (ub4) strlen(mySql),
                                OCI_NTV_SYNTAX, OCI_DEFAULT));

printf("OCIDefineByPos 1\n");
checkerr(p_err, OCIDefineByPos(p_sql, &p_define1, p_err, 1,
                                (dvoid *) Ename, (sb4) 19,
                                SQLT_CHR, (dvoid *) 0,
                                (ub2 *)0,(ub2 *)0, OCI_DEFAULT));
                                           
printf("OCIDefineByPos 2\n");
checkerr(p_err, OCIDefineByPos(p_sql, &p_define2, p_err, 2,
                                (dvoid *) date9, (sword) 8,
                                SQLT_DAT, (dvoid *) 0,
                                (ub2 *)0,(ub2 *)0, OCI_DEFAULT));

printf("OCIDefineByPos 3\n");
checkerr(p_err, OCIDefineByPos(p_sql, &p_define3, p_err, 3,
                                (dvoid *) &hdate, (sword)sizeof(hdate),
                                SQLT_ODT, (dvoid *) 0,
                                (ub2 *)0,(ub2 *)0, OCI_DEFAULT));

printf("OCIStmtExecute\n");
checkerr(p_err, OCIStmtExecute(p_svc, p_sql, p_err, (ub4) 1,
                                (ub4) 0, (OCISnapshot *)
                                NULL, (OCISnapshot *) NULL,
                                (ub4) OCI_COMMIT_ON_SUCCESS));

printf(" Employee Name ----> %s\n", Ename);
printf(" Hiredate - using internal date binary format\n");
printf(" ****************************************************\n");
printf(" * Century and Year are in an excess 100 notation. *\n");
printf(" ****************************************************\n");
printf(" Century --> %d\n", date9[0]);
printf(" Year --> %d\n", date9[1]);
printf(" Month --> %d\n", date9[2]);
printf(" Day --> %d\n", date9[3]);
printf(" ****************************************************\n");
printf(" * Hour, Min & Sec are in an excess 1 notation. *\n");
printf(" ****************************************************\n");
printf(" Hour --> %d\n", date9[4]);
printf(" Minute --> %d\n", date9[5]);
printf(" Second --> %d\n\n", date9[6]);


checkerr(p_err, OCIDateToText(p_err, (CONST OCIDate *) &hdate, (CONST text*)
            "Month dd, SYYYY, HH:MI A.M.",
            (ub1) 27, (CONST text*) "American",
                        (ub4) 8, (ub4 *)&str_len, hdate_buf));
printf(" Hiredate - using OCIDate object\n");
printf(" Hiredate ----> %s\n", hdate_buf);

return;
}

static void checkerr(errhp, status)
OCIError *errhp;sword status;
{
  text errbuf[512];
  ub4 errcode;
  switch (status)
                {
                case OCI_SUCCESS:
                        break;
                case OCI_SUCCESS_WITH_INFO:
                        printf("Error - OCI_SUCCESS_WITH_INFO\n");
                        break;
                case OCI_NEED_DATA:
                        printf("Error - OCI_NEED_DATA\n");
                        break;
                case OCI_NO_DATA:
                        printf("Error - OCI_NO_DATA\n");
                        break;
                case OCI_ERROR:
                        OCIErrorGet ((dvoid *) errhp, (ub4) 1, (text *) NULL, &errcode,
                                        errbuf, (ub4) sizeof(errbuf), (ub4) OCI_HTYPE_ERROR);
                        printf("Error - %s\n", errbuf);
                        break;
                case OCI_INVALID_HANDLE:
                        printf("Error - OCI_INVALID_HANDLE\n");
                        break;
                case OCI_STILL_EXECUTING:
                        printf("Error - OCI_STILL_EXECUTE\n");
                        break;
                case OCI_CONTINUE:
                        printf("Error - OCI_CONTINUE\n");
                        break;
                default:
                        break;
                }
}
- - - - - - - - - - - - - - - - Code ends here - - - - - - - - - - - - - - - -


Sample Output
-------------
OCIInitialize
OCIEnvInit
OCIHandleAlloc
OCIHandleAlloc
OCIHandleAlloc
OCILogon
OCIStmtPrepare
     SELECT ename, hiredate, hiredate FROM EMP
OCIDefineByPos 1
OCIDefineByPos 2
OCIDefineByPos 3
OCIStmtExecute
     Employee Name ----> MyUpdate
     Hiredate - using internal date binary format
       ****************************************************
       * Century and Year are in an excess 100 notation. *
       ****************************************************
       Century --> 119
       Year --> 181
       Month --> 2
       Day --> 20
       ****************************************************
       * Hour, Min & Sec are in an excess 1 notation. *
       ****************************************************
       Hour --> 1
       Minute --> 1
       Second --> 1

    Hiredate - using OCIDate object
    Hiredate ----> February 20, 1981, 12:00 A.M.

Additional Search Words
-----------------------
OCIDate, SQLT_ODT, SQLT_DAT


 

阅读(4274) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~