阅读背景:

Oracle数据库操作(pro*c/c++方式)

来源:互联网 
//============================================================================  
// Name        : CExercise.pc  
// Author      : Haier  
// Version     : 0.1  
// Copyright   : Your copyright notice  
// Description : Connect Oracle in C by Pro*c, Ansi-style  
//===========================================================================

#include <stdio.h>
#include <string.h>
#include <sqlca.h>
#include <stdlib.h>
#include <sqlda.h>
#include <sqlcpr.h>

#define UNAME_LEN      20 
#define PWD_LEN        11 
 
/*
 * Use the precompiler typedef'ing capability to create
 * null-terminated strings for the authentication host
 * variables. 
 */
typedef char asciiz[PWD_LEN]; 

EXEC SQL TYPE asciiz IS CHARZ(PWD_LEN) REFERENCE; 
asciiz     username; 
asciiz     password;
asciiz     server; 

struct emp_info 
{ 
    asciiz     emp_name; 
    asciiz      salary; 
    asciiz      commission; 
}; 

void sql_error(char *msg) 
{ 
    char err_msg[512];
    size_t buf_len, msg_len;

    EXEC SQL WHENEVER SQLERROR CONTINUE;

    printf("\n%s\n", msg);

/* Call sqlglm() to get the complete text of the
 * error message.
 */
    buf_len = sizeof (err_msg);
    sqlglm(err_msg, &buf_len, &msg_len);
    printf("%.*s\n", msg_len, err_msg);

    EXEC SQL ROLLBACK RELEASE;
    exit(EXIT_FAILURE);
} 

void main() 
{ 
    struct emp_info *emp_rec_ptr; 

/* Allocate memory for emp_info struct. */ 
    if ((emp_rec_ptr = 
        (struct emp_info *) malloc(sizeof(struct emp_info))) == 0)
    { 
        fprintf(stderr, "Memory allocation error.\n"); 
        exit(EXIT_FAILURE); 
    } 
 
/* Connect to ORACLE. */ 
    strcpy(username, "so1"); 
    strcpy(password, "1qaz!QAZ");
    strcpy(server, "KFCS"); 
 
    EXEC SQL WHENEVER SQLERROR DO sql_error("ORACLE error--");
 
    EXEC SQL CONNECT :username IDENTIFIED BY :password USING :server; 
    printf("\nConnected to ORACLE as user: %s\n", username); 
 
/* Declare the cursor. All static SQL explicit cursors
 * contain SELECT commands. 'salespeople' is a SQL identifier,
 * not a (C) host variable.
 */
    EXEC SQL DECLARE salespeople CURSOR FOR 
        SELECT BILL_ID, PASSWORD, STATE 
            FROM SO1.INS_PROD_A 
            WHERE BILL_ID LIKE '15238075968'; 
 
/* Open the cursor. */
    EXEC SQL OPEN salespeople; 
 
/* Get ready to print results. */
    printf("\n\nThe results are as follows:\n\n");
    printf("BILL_ID      PASSWORD       STATE\n"); 
    printf("---------------------------------\n"); 
 
/* Loop, fetching all salesperson's statistics.
 * Cause the program to break the loop when no more
 * data can be retrieved on the cursor.
 */
    EXEC SQL WHENEVER NOT FOUND DO break; 

    for (;;) 
    { 
        EXEC SQL FETCH salespeople INTO :emp_rec_ptr; 
        printf("%s\t%s\t%s\n", emp_rec_ptr->emp_name, 
                emp_rec_ptr->salary, emp_rec_ptr->commission); 
    } 
 
/* Close the cursor. */
    EXEC SQL CLOSE salespeople; 
 
    printf("\nThe 1 row is selected\n\n");

    EXEC SQL COMMIT WORK RELEASE; 
    exit(EXIT_SUCCESS); 
} 
//=============================================



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: