Chinaunix首页 | 论坛 | 博客
  • 博客访问: 155205
  • 博文数量: 39
  • 博客积分: 2075
  • 博客等级: 大尉
  • 技术积分: 420
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-26 15:55
文章分类

全部博文(39)

文章存档

2014年(5)

2013年(1)

2012年(1)

2011年(12)

2010年(24)

我的朋友

分类: C/C++

2011-06-08 16:40:27

#include <stdio.h>
#include <string.h>
EXEC SQL INCLUDE sqlca;


struct {
    VARCHAR emp_name[30];
    float salary;
    float commission;
} emprec;
/*
Define an indicator struct to correspond to the host output struct. */

struct
{
    short emp_name_ind;
    short sal_ind;
    short comm_ind;
} emprec_ind;

/* Input host variable. */
int emp_number;
int total_queried;
/* Include the SQL Communications Area.
   You can use #include or EXEC SQL INCLUDE. */



int databaseopen()
{
        EXEC SQL BEGIN DECLARE SECTION;
                char username[20];
                char password[20];
                char db_string[20];
        EXEC SQL END DECLARE SECTION;

        strcpy(username,"agent");
        strcpy(password,"agent");
        strcpy(db_string,"oraagent");

        EXEC SQL CONNECT :username IDENTIFIED BY :password using :db_string;
        if(sqlca.sqlcode != 0)
        {
                printf("open database error[%d]\n",sqlca.sqlcode);
                return (-1);
        }
        return 0;
}



/* Declare error handling function. */
void sql_error();

main()
{
    EXEC SQL WHENEVER SQLERROR DO sql_error("ORACLE error--\n");
    char temp_char[32];
        if(databaseopen()<0){
             sql_error("database open error\n");
        }

/* Loop, selecting individual employee's results */
    total_queried = 0;
    for (;;)
    {
/* Break out of the inner loop when a
 * 1403 ("No data found") condition occurs.
 */

        EXEC SQL WHENEVER NOT FOUND DO break;
        for (;;)
        {
            emp_number = 0;
            printf("\nEnter employee number (0 to quit): ");
            gets(temp_char);
            emp_number = atoi(temp_char);
            if (emp_number == 0)
                break;
            EXEC SQL SELECT ename, sal, comm
                INTO :emprec INDICATOR :emprec_ind
                FROM EMP
                WHERE EMPNO = :emp_number;
/* Print data. */
            printf("\n\nEmployee\tSalary\t\tCommission\n");
            printf("--------\t------\t\t----------\n");
/* Null-terminate the output string data. */
            emprec.emp_name.arr[emprec.emp_name.len] = '\0';
            printf("%-8s\t%6.2f\t\t",
                emprec.emp_name.arr, emprec.salary);
            if (emprec_ind.comm_ind == -1)
                printf("NULL\n");
            else
                printf("%6.2f\n", emprec.commission);

            total_queried++;
        } /* end inner for (;;) */
        if (emp_number == 0) break;
        printf("\nNot a valid employee number - try again.\n");
    } /* end outer for(;;) */

    printf("\n\nTotal rows returned was %d.\n", total_queried);
    printf("\nG'day.\n\n\n");

/* Disconnect from ORACLE. */
    EXEC SQL COMMIT WORK RELEASE;
    exit(0);
}
void sql_error(msg)
char *msg;
{
    char err_msg[128];
    int buf_len, msg_len;

    EXEC SQL WHENEVER SQLERROR CONTINUE;
    printf("\n%s\n", msg);
    buf_len = sizeof (err_msg);
   /* sqlglm(err_msg, &buf_len, &msg_len);*/
    printf("%.*s\n", msg_len, err_msg);
    EXEC SQL ROLLBACK RELEASE;
    exit(1);
}

 


 

阅读(547) | 评论(0) | 转发(0) |
0

上一篇:排序

下一篇:j2ee笔试题目

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