0

我使用我的 C 代码中的 Pro*C 代码连接到 Oracle DB 以使用数组进行批量插入/更新。这是将 C 双精度数据插入到列为 NUMBER(18,7) 的表中的示例。以下代码片段有效吗?请建议,尤其是 7(十进制)是否正确用作 Oracle 数据类型?下面给出的代码片段类似于http://docs.oracle.com/cd/B19306_01/appdev.102/b14407/pc_15ody.htm#i7496

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

#define SIZE    5

/* connect string */
char *username = "userid/passwd";

/* dbltbl table has column dblval as NUMBER(18,7) */
char *sql_stmt =
"INSERT INTO dbltbl(dblval) VALUES (:d)";
int  size = SIZE;  /* must have a host variable too */

SQLDA   *binda;

double vals[SIZE];

/* Declare and initialize indicator vars. for dblval columns */
short   ind_dblval[SIZE] = {0,0,0,0,0};

main() 
{ 
    EXEC SQL WHENEVER SQLERROR GOTO sql_error; 

/* Connect */ 
    EXEC SQL CONNECT :username; 
    printf("Connected.\n"); 

/* Allocate the descriptors and set the N component. 
   This must be done before the DESCRIBE. */ 
    binda = SQLSQLDAAlloc(SQL_SINGLE_RCTX, 1, 0, 0); 
    binda->N = 1; 

/* Prepare and describe the SQL statement. */ 
    EXEC SQL PREPARE stmt FROM :sql_stmt; 
    EXEC SQL DESCRIBE BIND VARIABLES FOR stmt INTO binda; 

/* Initialize the descriptors. */ 
    binda->V[0] = (char *) vals; 
    binda->L[0] = (long) sizeof (double); 
    binda->T[0] = 7;  /* Decimal */
    binda->I[2] = ind_dept; 

/* Initialize the data buffers. */ 
    vals[0] = 11.2; 
    vals[1] = 10.2; 
    vals[2] = 10.7; 
    vals[3] = 1.2; 
    vals[4] = 114.2; 

/* Do the INSERT. */
    printf("Adding ...\n");

    EXEC SQL FOR :size
    EXECUTE stmt USING DESCRIPTOR binda;

/*  Print rows-processed count. */
    printf("%d rows inserted.\n\n", sqlca.sqlerrd[2]);
    EXEC SQL COMMIT RELEASE;
    exit(0);

sql_error: 
/* Print Oracle error message. */
    printf("\n%.70s", sqlca.sqlerrm.sqlerrmc);
    EXEC SQL WHENEVER SQLERROR CONTINUE; 
    EXEC SQL ROLLBACK RELEASE; 
    exit(1); 
} 
4

0 回答 0