Chinaunix首页 | 论坛 | 博客
  • 博客访问: 285935
  • 博文数量: 124
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 21
  • 用 户 组: 普通用户
  • 注册时间: 2016-08-20 14:44
文章分类

全部博文(124)

文章存档

2020年(1)

2018年(2)

2016年(2)

2015年(6)

2014年(10)

2013年(23)

2012年(7)

2011年(18)

2010年(15)

2009年(8)

2007年(8)

2006年(23)

2005年(1)

我的朋友

分类: C/C++

2006-04-28 17:25:55

 //学习oracle编程。
#include
#include"oci.h"
#include
#define MAXROWS 4
void checkerr(OCIError *errhp,sword  status)
{
    text errbuf[512];
    sb4 errcode = 0;
   
    switch (status)
    {
    case OCI_SUCCESS:
        break;
    case OCI_SUCCESS_WITH_INFO:
        (void) printf("Error - OCI_SUCCESS_WITH_INFO\n");
        break;
    case OCI_NEED_DATA:
        (void) printf("Error - OCI_NEED_DATA\n");
        break;
    case OCI_NO_DATA:
        (void) printf("Error - OCI_NODATA\n");
        break;
    case OCI_ERROR:
        (void) OCIErrorGet((dvoid *)errhp, (ub4) 1, (text *) NULL, &errcode,
            errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR);
        (void) printf("Error - %.*s\n", 512, errbuf);
        break;
    case OCI_INVALID_HANDLE:
        (void) printf("Error - OCI_INVALID_HANDLE\n");
        break;
    case OCI_STILL_EXECUTING:
        (void) printf("Error - OCI_STILL_EXECUTE\n");
        break;
    case OCI_CONTINUE:
        (void) printf("Error - OCI_CONTINUE\n");
        break;
    default:
        break;
    }
} /* end checkerr() */
sword cleanup(boolean loggedon,OCIEnv    * envhp,OCISvcCtx * svchp,OCIError  * errhp)
{
   
   
    if (loggedon)
        OCILogoff (svchp, errhp);
   
    printf("Freeing handles ...\n");
   
    if (envhp)
        OCIHandleFree((dvoid *) envhp, (ub4) OCI_HTYPE_ENV);
   
    return OCI_SUCCESS;
}
void create_table(OCISvcCtx * svchp,OCIError  * errhp,OCIStmt   * select_p)
{
    char * mySql="create table tele( tel_number char(15),reply int)";
    sword errr;
    checkerr(errhp,OCIStmtPrepare (select_p, errhp,
        mySql, strlen(mySql), OCI_NTV_SYNTAX, OCI_DEFAULT));
    checkerr(errhp,OCIStmtExecute(svchp,
        select_p,
        errhp,
        (ub4) 1,
        (ub4) 0,
        (OCISnapshot *) NULL,
        (OCISnapshot *) NULL,
        (ub4) OCI_DEFAULT));
   


}
void insert(OCISvcCtx * svchp,OCIError  * errhp,OCIStmt   * select_p){
    char *mySql ="insert into college values('wanjm',100)";
    sword errr;
    checkerr(errhp,OCIStmtPrepare (select_p, errhp,
        mySql, strlen(mySql), OCI_NTV_SYNTAX, OCI_DEFAULT));
    checkerr(errhp,OCIStmtExecute(svchp,
        select_p,
        errhp,
        (ub4) 1, //此处花了我好多时间啊。
        (ub4) 0,
        (OCISnapshot *) NULL,
        (OCISnapshot *) NULL,
        (ub4) OCI_DEFAULT));
}
sword get_all_rows(OCISvcCtx *svchp,OCIError  * errhp,OCIStmt   * select_p,OCIRowid  ** Rowid)
{
   
   
    //  text *mySql = (text *) "SELECT C1, C2 from FOO where C1 > 15 for UPDATE";
    text *mySql = (text *) "SELECT name,age from college";
   
    ub4   prefetch = 2;
    sword errr;
    ub4   c2;
    text  c1[30];
    OCIDefine *defnp1, *defnp2;
    int  i;
    /* prepare the select statement for fetching all rows */
   
    if (OCIStmtPrepare (select_p, errhp,
        mySql, strlen(mySql), OCI_NTV_SYNTAX, OCI_DEFAULT))
    {
        printf ("Prepare failed \n");
        return (OCI_ERROR);
    }
   
    /*
    * since we are interested in all the rows lets set prefetch to 10
    * before execute
    */
   
   
//    if (OCIAttrSet (select_p,
//        OCI_HTYPE_STMT,
//        &prefetch,                        /* prefetch upto 10 rows */
//        0,
//        OCI_ATTR_PREFETCH_ROWS,
//        errhp))
//    {
//        printf ("Setting the prefetch count failed \n");
//        return (OCI_ERROR);
//    }
   
   
    if (errr = OCIStmtExecute(svchp,
        select_p,
        errhp,
        (ub4) 0,
        (ub4) 0,
        (OCISnapshot *) NULL,
        (OCISnapshot *) NULL,
        (ub4) OCI_DEFAULT))
    {
        if (errr != OCI_NO_DATA) return errr;
       
    }
   
   
    /*
    * now fetch all the rows.
    * we will just define one piece of storage each for the columns and get the
    * data into it since we really do not care for the column data.
    * after each fetch we will retrieve the rowid of the fetched row.
    */
   
    if (OCIDefineByPos ( select_p,
        &defnp1,
        errhp,
        1,
        &c1,
        30,
        SQLT_STR,
        (dvoid *) 0,
        (dvoid *) 0,
        (dvoid *) 0,
        OCI_DEFAULT) ||
        OCIDefineByPos ( select_p,
        &defnp2,
        errhp,
        2,
        &c2,
        sizeof (c2),
        SQLT_INT,
        (dvoid *) 0,
        (dvoid *) 0,
        (dvoid *) 0,
        OCI_DEFAULT))
    {
        printf ("Failed to define\n");
        return (OCI_ERROR);
    }
   
    printf ("Column C1     Column C2\n");
    printf ("_______________________\n");
   
    for (i = 0; i < MAXROWS; i++)
    {
        if (OCIStmtFetch (select_p,
            errhp,
            1,
            OCI_FETCH_NEXT,
            OCI_DEFAULT))
        {
         //   printf ("Fetch failed \n");
            return (OCI_SUCCESS);
        }
       
        printf ("%s            %d\n", c1, c2);
    }
   
    return OCI_SUCCESS;
}


int main()
{
    char *username = "wanjm";
    char *password = "wanjm";
    boolean logged_on = FALSE;
//    int i;
    OCIEnv    *envhp;
    OCISvcCtx *svchp;
    OCIError  *errhp;
//    OCIRowid *Rowid[MAXROWS];
    OCIStmt *select_p;
    //    if (OCIInitialize((ub4)OCI_DEFAULT, (dvoid *)0,
    //        (dvoid * (*)(dvoid *, size_t)) 0,
    //        (dvoid * (*)(dvoid *, dvoid *, size_t))0,
    //        (void (*)(dvoid *, dvoid *)) 0 ))
    //    {
    //        printf("FAILED: OCIInitialize()\n");
    //        return OCI_ERROR;
    //    }
    if (OCIEnvCreate((OCIEnv **)&envhp,
        (ub4)OCI_THREADED|OCI_OBJECT, (dvoid *)0,
        (dvoid * (*)(dvoid *, size_t)) 0,
        (dvoid * (*)(dvoid *, dvoid *, size_t))0,
        (void (*)(dvoid *, dvoid *)) 0,
        (size_t) 0, (dvoid **) 0 ))
        printf("FAILED: OCIEnvCreate()\n");
//    if (OCIEnvInit(&envhp, (ub4) OCI_DEFAULT,
//        (size_t) 0, (dvoid **) 0 ))
//    {
//        printf("FAILED: OCIEnvInit()\n");
//        return OCI_ERROR;
//    }
   
    if (OCIHandleAlloc(envhp, &errhp,
        (ub4) OCI_HTYPE_ERROR, (size_t) 0, (dvoid **) 0))
    {
        printf("FAILED: OCIHandleAlloc() on errhp\n");
        return OCI_ERROR;
    }
    if (OCIHandleAlloc((dvoid *) envhp,
        (dvoid **) &svchp,
        (ub4) OCI_HTYPE_SVCCTX, (size_t) 0, (dvoid **) 0))
        printf("FAILED: OCIHandleAlloc() on ctxptr->svchp\n");
   
   
    if (OCILogon(envhp, errhp, &svchp, username, strlen (username),
        password, strlen (password), "data", strlen("data")))
    {
        printf("FAILED: log_on()\n");
        return 1;
        return cleanup(logged_on, envhp, svchp, errhp);
    }
    logged_on=TRUE;
    if (OCIHandleAlloc((dvoid *)envhp, (dvoid **) &select_p,
        (ub4)OCI_HTYPE_STMT, (CONST size_t) 0, (dvoid **) 0))
    {
        printf("FAILED: alloc statement handles\n");
        return 1;
    }
    create_table(svchp, errhp, select_p);
    if (get_all_rows(svchp, errhp, select_p, 0))
    {
        printf("Fetch of all rowids failed\n");
        return cleanup(logged_on, envhp, svchp, errhp);
    }
    return cleanup(logged_on, envhp, svchp, errhp);
}
用命令
 gcc  -I/home/wanjm/OraHome1/rdbms/demo -I/home/wanjm/OraHome1/rdbms/public  select.c -L/home/wanjm/OraHome1/lib -lclntsh
编译运行.

实际运行是会可能报找不到lib库,以root用户打开/etc/ld.so.conf文件,添加lib的路径.然后运行ldconfig命令即可生效.

阅读(2678) | 评论(0) | 转发(1) |
0

上一篇:ANSI控制码

下一篇:linux下动态链接库入门

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