免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4585 | 回复: 10
打印 上一主题 下一主题

pro*c的高手看过来 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-09-08 13:19 |只看该作者 |倒序浏览
20可用积分
本帖最后由 cups_book 于 2010-09-08 14:20 编辑

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);
    if (msg_len > buf_len)
    msg_len = buf_len;
    printf("%.*s\n", msg_len, err_msg);
    EXEC SQL ROLLBACK RELEASE;
    exit(1);
}

解释一下上面的函数

蓝色部分,看起来很怪,看不懂。

我改成:
/* declare error handling function*/
void sql_error(char *msg)
{
        char err_msg[128];
        size_t 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);
}
也是可以顺利编译的。
[oracle@Tux10M simpapp]$ proc parse=none iname=fetch.pc include=/u01/app/oracle/product/10.2.0/client_1/precomp/public

Pro*C/C++: Release 10.2.0.1.0 - Production on Wed Sep 8 13:56:05 2010

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

System default option values taken from: /u01/app/oracle/product/10.2.0/client_1/precomp/admin/pcscfg.cfg

[oracle@Tux10M simpapp]$

最佳答案

论坛徽章:
2
水瓶座
日期:2013-09-04 15:09:57白羊座
日期:2014-04-17 16:48:13
2 [报告]
发表于 2010-09-08 13:19 |只看该作者
等于
  1. void sql_error(char *msg)
复制代码

论坛徽章:
0
3 [报告]
发表于 2010-09-08 13:20 |只看该作者

orcle的官方文档来的,是正式文档

Example Program: A Simple Query
One way to get acquainted with Pro*C/C++ and embedded SQL is to study a program example. The following program is also available on-line in the file sample1.pc in your Pro*C/C++ demo directory.

The program connects to Oracle, then loops, prompting the user for an employee number. It queries the database for the employee's name, salary, and commission, displays the information, and then continues the loop. The information is returned to a host structure. There is also a parallel indicator structure to signal whether any of the output values SELECTed might be NULL.

Precompile example programs using the precompiler option MODE=ORACLE.

/*
*  sample1.pc
*
*  Prompts the user for an employee number,
*  then queries the emp table for the employee's
*  name, salary and commission.  Uses indicator
*  variables (in an indicator struct) to determine
*  if the commission is NULL.
*
*/

#include <stdio.h>
#include <string.h>


/* Define constants for VARCHAR lengths. */
#define     UNAME_LEN      20
#define     PWD_LEN        40

/* Declare variables.No declare section is needed if MODE=ORACLE.*/
VARCHAR     username[UNAME_LEN];  
/* VARCHAR is an Oracle-supplied struct */
varchar     password[PWD_LEN];   
/* varchar can be in lower case also. */
/*
Define a host structure for the output values of a SELECT statement.
*/
struct {
    VARCHAR   emp_name[UNAME_LEN];
    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. */
#include <sqlca.h>

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

main()
{
    char temp_char[32];

/* Connect to ORACLE--
* Copy the username into the VARCHAR.
*/
    strncpy((char *) username.arr, "SCOTT", UNAME_LEN);
/* Set the length component of the VARCHAR. */
    username.len = strlen((char *) username.arr);
/* Copy the password. */
    strncpy((char *) password.arr, "TIGER", PWD_LEN);
    password.len = strlen((char *) password.arr);
/* Register sql_error() as the error handler. */
    EXEC SQL WHENEVER SQLERROR DO sql_error("ORACLE error--\n");

/* Connect to ORACLE.  Program will call sql_error()
* if an error occurs when connecting to the default database.
*/
    EXEC SQL CONNECT :username IDENTIFIED BY :password;
    printf("\nConnected to ORACLE as user: %s\n", username.arr);
/* 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, NVL(comm, 0)
                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);
    if (msg_len > buf_len)
    msg_len = buf_len;
    printf("%.*s\n", msg_len, err_msg);
    EXEC SQL ROLLBACK RELEASE;
    exit(1);
}

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
4 [报告]
发表于 2010-09-08 13:46 |只看该作者
为什么不写在括号里,要写出来呢。C被ORACLE整得是乱七八糟的。

论坛徽章:
0
5 [报告]
发表于 2010-09-08 13:49 |只看该作者
为什么不写在括号里,要写出来呢。C被ORACLE整得是乱七八糟的。
renxiao2003 发表于 2010-09-08 13:46



    对呵, 我就是被它搞的一头雾水。

论坛徽章:
0
6 [报告]
发表于 2010-09-08 13:50 |只看该作者
等于
l2y3n2 发表于 2010-09-08 13:41



   
确认不?

论坛徽章:
3
CU大牛徽章
日期:2013-09-18 15:16:55CU大牛徽章
日期:2013-09-18 15:18:22CU大牛徽章
日期:2013-09-18 15:18:43
7 [报告]
发表于 2010-09-08 14:06 |只看该作者
不太懂  帮顶啊  呵呵呵

论坛徽章:
0
8 [报告]
发表于 2010-09-08 14:21 |只看该作者
回复 7# duolanshizhe


   
呵呵,对了,你研究出怎么查看pro*c的版本了吗?

论坛徽章:
3
CU大牛徽章
日期:2013-09-18 15:16:55CU大牛徽章
日期:2013-09-18 15:18:22CU大牛徽章
日期:2013-09-18 15:18:43
9 [报告]
发表于 2010-09-08 14:30 |只看该作者
proc --version

论坛徽章:
2
水瓶座
日期:2013-09-04 15:09:57白羊座
日期:2014-04-17 16:48:13
10 [报告]
发表于 2010-09-08 16:03 |只看该作者
确认不?
cups_book 发表于 2010-09-08 13:50



    不确定,pro*c什么的不了解。

但是GCC是支持这种函数的声明方式的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP