程式碼示例:
/ 定義符號常數
#define USERNAME "SCOTT"
#define PASSWORD "x"
#include <stdio.h>
// 說明SQLCA和ORACA
EXEC SQL INCLUDE SQLCA;
EXEC SQL INCLUDE ORACA;
// 啟用ORACLE通訊區:ORACA=YES,使它能被使用
EXEC ORACLE OPTION (ORACA=YES);
// 說明SQL變數
EXEC SQL BEGIN DECLARE SECTION;
char* username=USERNAME;
char* password=PASSWORD;
VARCHAR sqlstmt[80];
int emp_number;
VARCHAR emp_name[15];
VARCHAR job[50],job1[50],job2[50];
float salary;
EXEC SQL END DECLARE SECTION;
main()
{
EXEC SQL WHENEVER SQLERROR GOTO sqlerror;
// 發生錯誤時,儲存SQL語句至ORACA
oraca.orastxtf=ORASTFERR;
// 登入到ORACLE
EXEC SQL CONNECT :username IDENTIFIED BY :password;
printf("/nConnect to ORACLE./n");
// 構造動態SQL語句
sqlstmt.len=sprintf(sqlstmt.arr,"INSERT INTO EMP(EMPNO,ENAME,JOB,SAL)VALUES(:V1,:V2,:V3,:V4)");
// 顯示SQL語句
puts(sqlstmt.arr);
// 用PREPARE語句分析當前的動態INSERT語句,語句名是S
EXEC SQL PREPARE S FROM :sqlstmt;
// 迴圈插表
for(;;)
printf("/nEnter employee number:");
scanf("%d",&emp_number);
if(emp_number==0)break;
printf("/nEnter employee name:");
scanf("%s",&emp_name.arr);
emp_name.len=strlen(emp_name.arr);
printf("/nEnter employee job:");
scanf("%s",&job.arr);
job.len=strlen(job.arr);
salary = 0; // With VC6, Missing this line will cause C Run-Time Error R6002.
printf("/nEnter salary:");
scanf("%f",&salary);
EXEC SQL EXECUTE S USING :emp_number,:emp_name,:job,:salary;
}
// 提交事務,退出ORACLE
EXEC SQL COMMIT RELEASE;
printf("/nHave a good day!/n");
exit(0);
sqlerror:
// 列印錯誤資訊
printf("/n%.*s/n",sqlca.sqlerrm.sqlerrml,sqlca.sqlerrm.sqlerrmc);
// 打印出錯SQL語句
printf("/n/"%.*s.../"/n",oraca.orastxt.orastxtl,oraca.orastxt.orastxtc);
// 打印出錯SQL語句所在行號及所在檔名
printf("on line %d of %.*s/n/n",oraca.oraslnr,
oraca.orasfnm.orasfnml,oraca.orasfnm.orasfnmc);
程式碼示例:
/ 定義符號常數
#define USERNAME "SCOTT"
#define PASSWORD "x"
#include <stdio.h>
// 說明SQLCA和ORACA
EXEC SQL INCLUDE SQLCA;
EXEC SQL INCLUDE ORACA;
// 啟用ORACLE通訊區:ORACA=YES,使它能被使用
EXEC ORACLE OPTION (ORACA=YES);
// 說明SQL變數
EXEC SQL BEGIN DECLARE SECTION;
char* username=USERNAME;
char* password=PASSWORD;
VARCHAR sqlstmt[80];
int emp_number;
VARCHAR emp_name[15];
VARCHAR job[50],job1[50],job2[50];
float salary;
EXEC SQL END DECLARE SECTION;
main()
{
EXEC SQL WHENEVER SQLERROR GOTO sqlerror;
// 發生錯誤時,儲存SQL語句至ORACA
oraca.orastxtf=ORASTFERR;
// 登入到ORACLE
EXEC SQL CONNECT :username IDENTIFIED BY :password;
printf("/nConnect to ORACLE./n");
// 構造動態SQL語句
sqlstmt.len=sprintf(sqlstmt.arr,"INSERT INTO EMP(EMPNO,ENAME,JOB,SAL)VALUES(:V1,:V2,:V3,:V4)");
// 顯示SQL語句
puts(sqlstmt.arr);
// 用PREPARE語句分析當前的動態INSERT語句,語句名是S
EXEC SQL PREPARE S FROM :sqlstmt;
// 迴圈插表
for(;;)
{
printf("/nEnter employee number:");
scanf("%d",&emp_number);
if(emp_number==0)break;
printf("/nEnter employee name:");
scanf("%s",&emp_name.arr);
emp_name.len=strlen(emp_name.arr);
printf("/nEnter employee job:");
scanf("%s",&job.arr);
job.len=strlen(job.arr);
salary = 0; // With VC6, Missing this line will cause C Run-Time Error R6002.
printf("/nEnter salary:");
scanf("%f",&salary);
EXEC SQL EXECUTE S USING :emp_number,:emp_name,:job,:salary;
}
// 提交事務,退出ORACLE
EXEC SQL COMMIT RELEASE;
printf("/nHave a good day!/n");
exit(0);
sqlerror:
// 列印錯誤資訊
printf("/n%.*s/n",sqlca.sqlerrm.sqlerrml,sqlca.sqlerrm.sqlerrmc);
// 打印出錯SQL語句
printf("/n/"%.*s.../"/n",oraca.orastxt.orastxtl,oraca.orastxt.orastxtc);
// 打印出錯SQL語句所在行號及所在檔名
printf("on line %d of %.*s/n/n",oraca.oraslnr,
oraca.orasfnm.orasfnml,oraca.orasfnm.orasfnmc);