在这里我先说遇到的一个问题
在练习的例子中加入了头文件
#include <oraca.h>
然后我怎么编译都不行,一直说找不到oraca.h
然后我把这个改成了
EXEC SQL INCLUDE oraca;
这样就顺利的编译过去了。
第一种动态SQL语句:无宿主变量的非查询语句
应用程序接收外界输入构造SQL 语句到一个字符串,然后调用
EXECUTE IMMEDIATE 命令执行这个SQL 语句。这种SQL 语句不能为SELECT 语句,不
能包含输入变量的占位符。
5.pc #include <stdio.h> #include <string.h>
EXEC SQL INCLUDE sqlca; EXEC SQL INCLUDE oraca; EXEC ORACLE OPTION(ORACA=YES); EXEC ORACLE OPTION(RELEASE_CURSOR=YES);
int main(int argc, char *argv[]) { EXEC SQL BEGIN DECLARE SECTION; char *username = "scott"; char *passwd = "xx"; char *dynstmt1; char dynstmt2[10]; VARCHAR dynstmt3[80]; char dynstmt4[80]; EXEC SQL END DECLARE SECTION;
oraca.orastxtf = ORASTFERR;
EXEC SQL CONNECT :username IDENTIFIED BY :passwd; puts("CREATE TABLE dyn1 (col1 VARCHAR2(4))"); EXEC SQL EXECUTE IMMEDIATE "CREATE TABLE dyn1 (col1 VARCHAR2(4))";
dynstmt1 = "INSERT INTO dyn1 VALUES('TEST')"; puts(dynstmt1); EXEC SQL EXECUTE IMMEDIATE :dynstmt1;
strncpy(dynstmt2, "COMMIT", 10); printf("%.10s\n", dynstmt2); EXEC SQL EXECUTE IMMEDIATE :dynstmt2;
EXEC SQL COMMIT;
/*这里我自己又完全输入一个 DROP TABLE dyn1 这个字符串,来试试*/ gets(dynstmt4); EXEC SQL EXECUTE IMMEDIATE :dynstmt4; EXEC SQL COMMIT WORK RELEASE;
return 0; }
第二种:已知输入变量个数的非查询语句
应用程序接受或组建一个SQL 语句,并通过PREPARE 和EXECUTE 语
句执行这个SQL。不能是查询语句。为输入宿主变量准备的占位符数目和输入宿主变量的
类型必须已知
6.pc #include <stdio.h> #include <string.h>
EXEC SQL BEGIN DECLARE SECTION; #define USERNAME "scott" #define PASSWD "xx"
EXEC SQL INCLUDE sqlca; EXEC SQL INCLUDE oraca; EXEC ORACLE OPTION(ORACA=YES); char *username = USERNAME; char *passwd = PASSWD; VARCHAR dynstmt[80]; int empno = 1234; int deptno1 = 20; int deptno2 = 30; EXEC SQL END DECLARE SECTION;
int main(int argc, char *argv[]) { oraca.orastxtf = ORASTFERR;
EXEC SQL CONNECT :username IDENTIFIED BY :passwd;
strcpy(dynstmt.arr ,"INSERT INTO EMP (EMPNO, DEPTNO) VALUES (:v1, :v2)"); dynstmt.len = strlen(dynstmt.arr);
puts((char *)dynstmt.arr); printf("v1 = %d, v2 = %d\n", empno, deptno1); EXEC SQL PREPARE insert_sql FROM :dynstmt; EXEC SQL EXECUTE insert_sql USING :empno, :deptno1; EXEC SQL COMMIT;
empno ++; EXEC SQL EXECUTE insert_sql USING :empno, :deptno2; printf("v1 = %d, v2 = %d\n", empno, deptno2); EXEC SQL COMMIT;
strcpy(dynstmt.arr, "DELETE FROM EMP WHERE EMPNO = :v1"); dynstmt.len = strlen(dynstmt.arr); /* 这里我还是通过从终端输入一个值来执行 */ char temp[10]; gets(temp); empno = atoi(temp);
EXEC SQL PREPARE delete_sql FROM :dynstmt; EXEC SQL EXECUTE delete_sql USING :empno; EXEC SQL COMMIT WORK RELEASE;
return 0; }
这个就是语句中带有宿主变量的,也就是说,我们一开始不知道要删除哪个,后来通过别的渠道给定要删除的这个条件。
第三种: 通过接受或构建SQL 语句,用DELCARE、PREPARE 和OPEN、FETCH、CLOSE 语句执行动态查询SQL 语句,
并访问查询结构。
适用于已知 SELECT 查询语句查询的各列、列类型、输入宿主变量数目、输入宿主变
量类型的情况
7.pc #include <stdio.h> #include <string.h>
EXEC SQL BEGIN DECLARE SECTION;
EXEC SQL INCLUDE sqlca; EXEC SQL INCLUDE oraca; EXEC ORACLE OPTION(ORACA=YES);
#define USERNAME "scott" #define PASSWD "xx"
char *username = USERNAME; char *passwd = PASSWD; VARCHAR dynstmt[80]; VARCHAR ename[10];
EXEC SQL END DECLARE SECTION;
int main(int argc, char *argv[]) { EXEC SQL BEGIN DECLARE SECTION; int deptno = 10; EXEC SQL END DECLARE SECTION;
oraca.orastxtf = ORASTFERR;
EXEC SQL CONNECT :username IDENTIFIED BY :passwd;
strcpy(dynstmt.arr, "SELECT ename FROM EMP WHERE deptno = :v1"); dynstmt.len = strlen(dynstmt.arr);
EXEC SQL PREPARE select_sql FROM :dynstmt; EXEC SQL DECLARE select_cur CURSOR FOR select_sql; /*自己输入要找的条件来查找*/ char temp[10]; gets(temp); deptno = atoi(temp); EXEC SQL OPEN select_cur USING :deptno;
EXEC SQL WHENEVER NOT FOUND DO break;
while (1) { EXEC SQL FETCH select_cur INTO :ename; ename.arr[ename.len] = '\0'; puts((char *)ename.arr); } printf("\nQuery returned %d row %s.\n\n", sqlca.sqlerrd[2], (sqlca.sqlerrd[2] == 1)?"":"s");
EXEC SQL CLOSE select_cur; EXEC SQL COMMIT WORK RELEASE;
return 0; }