/******************************************************************************
 * Pro* C/C++ SELECT INSERT 演示程序
 *
 * 向joe_t2表插入测试数据,表生成准备脚本见当前目录ctScript.sql
 *
 * 黑龙江省哈尔滨市平房区
 * 作者:高宏伟(DukeJoe)
 * 2005-3-21 16:10
 ******************************************************************************/
#include <stdio.h>
#include <sqlca.h>
#include <stdarg.h>
#include <time.h>
#include <string.h>#define     UNAME_LEN 20
#define     PWD_LEN 40
#define BUFFER_LENGTH 1024void sql_error(char *msg) ;
void TRACE(char * pMsg) ;
int QueryCount() ;
int InsertData(int iMaxF1, int iMaxF2) ;int main()
{
/* Declare variables.No declare section is needed if MODE=ORACLE.*/
VARCHAR     username[UNAME_LEN];  
varchar     password[PWD_LEN];    
time_t tmBegin, tmEnd ;
char * pc ;
char szBuffer[BUFFER_LENGTH] ;
const int iMaxF1 = 10 ;
const int iMaxF2 = 5 ; time(&tmBegin) ;

/* 用户名口令 */
    strncpy((char *) username.arr, "billing", UNAME_LEN);
    username.len = strlen((char *) username.arr);
    strncpy((char *) password.arr, "billing", PWD_LEN);
    password.len = strlen((char *) password.arr);
    /* 注册sql_error为错误处理函数 */
    EXEC SQL WHENEVER SQLERROR DO sql_error("ORACLE error--\n");
    /* 连接数据库 */
    EXEC SQL CONNECT :username IDENTIFIED BY :password;
    sprintf(szBuffer, "\nConnected to ORACLE as user: %s\n", username.arr);
    
    /**************************************************************************
     * 为保证程序的连贯性,不必在SQL Plus中经常truncate table,
     * 在程序中加入查询和DELETE的功能
     *************************************************************************/
    /* 查询当前的记录数 */
QueryCount() ;
InsertData(iMaxF1, iMaxF2) ;

    EXEC SQL COMMIT WORK RELEASE; time(&tmEnd) ;
pc = ctime(&tmBegin) ;
printf("\n开始时间:%s\n", pc) ;
pc = ctime(&tmEnd) ;
printf("结束时间:%s\n", pc) ; return 0 ;
}/******************************************************************************
 * 系统跟踪函数(仿Microsoft Visual C++ TRACE 宏)
 * v1.0
 *****************************************************************************/
void TRACE(char * pMsg)
{
time_t ttSimple ;
char szBuffer[64] ;
struct tm * ptmShow ; time(&ttSimple) ;
ptmShow = localtime(&ttSimple) ;

strftime(szBuffer, 64, "%Y-%m-%d %H:%M:%S", ptmShow) ;
printf("Logging(%s)-->%s\n", szBuffer, pMsg) ;
}int QueryCount()
{
int iCount = 0 ;    EXEC SQL SELECT count(*) INTO :iCount
     FROM joe_t2 ;
    
    if ( iCount > 0 )
    {
     char szBuffer[16] ;
     printf("在表joe_t2中发现%d条记录,如果不删除,可能因为主键约束的原因无法插入新值"
     "。是否删除(y/n)?[Y]\t", iCount) ;
     gets(szBuffer) ;
     switch( *szBuffer )
     {
     case 'n':
     case 'N':
     break ;
     default :
     EXEC SQL DELETE FROM joe_t2 ;
    }
    }
    
    return 0 ;
}int InsertData(int iMaxF1, int iMaxF2)
{
int iF1, iF2 ;
char szBuffer[BUFFER_LENGTH] ; /* 插入数据 */
for ( iF1 = 0 ; iF1 < iMaxF1 ; iF1++ )
{
for ( iF2 = 0 ; iF2 < iMaxF2 ; iF2++ )
{
sprintf(szBuffer, "第 %3d 条记录", iF1*iMaxF2+iF2) ;
TRACE(szBuffer) ;
EXEC SQL INSERT INTO joe_t2(f1, f2, f3, f4)
VALUES ( :iF1, :iF2, :szBuffer, sysdate) ;
}
}

return 0 ;
}/******************************************************************************
 * 错误处理函数
 *****************************************************************************/
void sql_error(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);
}
/* 这是我前几天写的一个测试的小程序,虽然还有一些BUG,但足够来演示Pro*C/C++的一些基本技术了。看懂这个小程序,你的问题就自然可以得到解决。在VC中要操纵ORACLE一般使用的是ADO和ODBC。这是普通的开发习惯,向这个习惯靠拢将可能有助于解释你现在的困惑,不然,你可能会问题不断。祝你好运。*/