Chinaunix首页 | 论坛 | 博客
  • 博客访问: 606223
  • 博文数量: 30
  • 博客积分: 8020
  • 博客等级: 中将
  • 技术积分: 1125
  • 用 户 组: 普通用户
  • 注册时间: 2007-01-31 16:14
文章分类

全部博文(30)

文章存档

2011年(1)

2010年(3)

2009年(5)

2008年(21)

分类: LINUX

2009-06-05 09:56:59

   PROC编程相信在Linux下工作的朋友应该不陌生吧!下面就来具体介绍些Proc编程相关的东西,此文相对简单,请高手们绕行。

PROC编程篇
(此文仅供Oracle初学者参考只用)

/**********************************************
** 文章名称:Oracle10G--PROC编程篇(Linux)
** 版权信息:可以任意转载,但要注明出处及作者信息
** 作   者: 隗公万
** 时   间: 2009.06.05
** 联系方式:weigongwan@gmail.com
***********************************************/

一、简单取得系统时间

#include
#include
#include

EXEC SQL INCLUDE sqlca;
EXEC SQL INCLUDE oraca;

main( int argc, char *argv[] )
{
        EXEC SQL BEGIN DECLARE SECTION;
                varchar vcBuffer[32];
        EXEC SQL END DECLARE SECTION;

        /* Connect To Oracle Datebaase */
        if(OU_ConnectDatabase("weigw","weigw") != 0)
        {
                printf("Connect Database failed!\n");

                return -1;

        }
        memset(vcBuffer.arr, 0x00, sizeof(vcBuffer.arr));
        EXEC SQL select sysdate into :vcBuffer from dual;
        printf("SYSDATE:<%s>\n", vcBuffer.arr);


        EXEC SQL COMMIT WORK RELEASE;

       return 0;

}

/*
**
** 功能说明:连接数据库
** 输入参数:
** 输入参数:
** 输出参数:
** 返回值说明:0-成功  其他-失败
*/
int OU_ConnectDatabase(char *con_name,char *con_passwd)
{
      EXEC SQL BEGIN DECLARE SECTION;
            VARCHAR uid[20];
            VARCHAR pwd[20];
       EXEC SQL END DECLARE SECTION;

        strcpy(uid.arr,con_name); /*strcpy(uid.arr,"weigw");*/
        uid.len=strlen(uid.arr);
        strcpy(pwd.arr,con_passwd); /*strcpy(pwd.arr,"weigw");*/
        pwd.len=strlen(pwd.arr);
        EXEC SQL CONNECT :uid IDENTIFIED BY :pwd ;

        return sqlca.sqlcode;
}



二、多库用户操作(排他锁)

#include
#include
#include

EXEC SQL INCLUDE sqlca;
EXEC SQL INCLUDE oraca;

main( int argc, char *argv[] )
{
        EXEC SQL BEGIN DECLARE SECTION;
                VARCHAR vcBuffer[32];

                int ID;

                const char *dbname1 = "dbname1";

                const char *dbname2 = "dbname2";

        EXEC SQL END DECLARE SECTION;

       
/* Connect To Oracle Datebaase dbname1 */
        if(OU_ConnectDatabase("weigw","weigw","orcl","dbname1") != 0)
        {
                printf("Connect Database failed!\n");

                return -1;

       }

       /* Connect To Oracle Datebaase dbname2 */
        if(OU_ConnectDatabase("weigw","weigw","orcl","dbname2") != 0)
        {
                printf("Connect Database dbname 2 failed!\n");

                return -1;

       }

       ID = 0;

       memset(vcBuffer.arr, 0x00, sizeof(vcBuffer.arr));

       vcBuffer.len = 0;


       EXEC SQL AT :dbname1 SELECT COUNT(*) INTO :ID FROM WEIGWTEST;
       ID += 1;

       strncpy(vcBuffer.arr, "dbname1", sizeof(vcBuffer.arr) - 1);

       vcBuffer.len = strlen(vcBuffer.arr);


       EXEC SQL AT :dbname1 INSERT INTO WEIGWTEST VALUES(:ID, :vcBuffer);

       EXEC SQL AT :dbname2 INSERT INTO WEIGWTEST VALUES(:ID, :vcBuffer);

       EXEC SQL AT :dbname1 COMMIT;

       EXEC SQL AT :dbname1 ROLLBACK;


       EXEC SQL COMMIT WORK RELEASE;

       return 0;
}

/*
**
** 功能说明:连接数据库
** 输入参数:
** 输入参数:
** 输出参数:
** 返回值说明:0-成功  其他-失败
*/
int
OU_ConnectDatabase(
    char *con_name,
    char *con_passwd,
   
char *con_string,
   
char *con_dbname
) {
      EXEC SQL BEGIN DECLARE SECTION;
            VARCHAR uid[20];
            VARCHAR pwd[20];
            VARCHAR sid[20];
            VARCHAR dbname[20];
       EXEC SQL END DECLARE SECTION;

       memset(uid.arr, 0x00, sizeof(uid.arr));
       uid.len = 0;
       memset(pwd.arr, 0x00, sizeof(pwd.arr));
       pwd.len = 0;
       memset(sid.arr, 0x00, sizeof(sid.arr));
       sid.len = 0;
       memset(dbname.arr, 0x00, sizeof(dbname.arr));
       dbname.len = 0;

        strcpy(uid.arr,con_name);
        uid.len=strlen(uid.arr);
        strcpy(pwd.arr,con_passwd);
        pwd.len=strlen(pwd.arr);
        strcpy(sid.arr,con_string);
        sid.len=strlen(sid.arr);
        strcpy(dbname.arr,con_dbname);
       
dbname.len=strlen(dbname.arr);

        EXEC SQL CONNECT :uid IDENTIFIED BY :pwd
AT :dbname USING :dbname;

        return sqlca.sqlcode;
}

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