C コード内から Pro*C コードを使用して Oracle DB に接続し、配列を使用して一括挿入/更新を行います。NUMBER(18,7) の列を持つテーブルに C の double データを挿入する例を次に示します。次のコード スニペットは有効ですか? 特に、Oracle データ型として使用するのに 7 (10 進数) は正しいですか? 以下のコード スニペットは、 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);
}